0

i am a newbie in firebase firestore. i am trying to write this the following data below to my firestore database:

    export const testingOnly = functions.database.ref('users/{userID}/')
.onCreate((snapshot, context) => {
    let addDoc = {
        c_username: 'Amanda',
        c_password: 'adamma21',
        isEmailConfirmed: true,
        isTelephoneNumberConfirmed: false
    }
    return snapshot.ref.set(addDoc);
});

Following a tut, i ran the following on the terminal:

npm run-script build

firebase functions: shell

It showed me this:

 C:\brighterbrains\functions> npm run-script build

> functions@ build C:\brighterbrains\functions
> tsc

PS C:\brighterbrains\functions> firebase functions:shell
!  Your requested "node" version "8" doesn't match your global version "12"       
+  functions: Emulator started at http://localhost:5000
i  functions: Loaded functions: testingOnly
firebase > testingOnly
[Function: bound ]
firebase >

i checked my Firestore db, it is still empty.

Am i doing this right? is there anything i am missing from my code?

i need help.

2 Answers 2

1

Note that it is not clear if your Cloud Function shall be triggered by an event in the Realtime Database (as shown by your code) or in Cloud Firestore (as shown in your question text).


The Firebase CLI includes a Cloud Functions emulator that allows to run Cloud Functions locally. But, at the time of writing, this emulator can only emulate the following function types, see the doc:

  • HTTPS functions
  • Callable functions
  • Cloud Firestore functions

So, since your Cloud Function seems to be triggered by events in the Realtime Database, you will need to deploy it, in order to run it. See this "Get Started" doc for more detail on how to proceed or watch the "Getting Started with Cloud Functions for Firebase using TypeScript" video, which uses TypeScript but is also valid for the CLI part for JavaScript.


On the other hand, if you actually want to trigger it by an event in Cloud Firestore, you should first change the code of your function (see the doc referred above) and then use the Emulator as explained in the doc (or deploy it).

Sign up to request clarification or add additional context in comments.

5 Comments

i will check it out and give u a response.
I followed your directions and from the page which happens to be the Firestore docs, i saw my function at the functions tab. What i intentionally want to do is, i run the function, it writes those values into my Firestore db. It didn't.
Like I said in my anwser your Cloud Function code is for the Realtime Database, not the Firestore Database. They are two different database Services offered by Firebase. You should look at firebase.google.com/docs/functions/firestore-events if you want to write to Firestore. If, after you adapted your code, you still encounter problems, you may ask a new question and inform me by writing a comment here, I'll have a look at your question.
Now i get what you are saying. Thanks and i will do so.
i rewrote my code but i am having an "Object unidentified" isue. I asked it as another question with subject: FIrebase Firestore onCreate Cloud Function: Object unidentified
0

There are some ways that you can write your data. For writing, the are two atomic operations that might be done, for you to write data in your Firestore, they are: Transactions and batched writes.

Considering that, I would recommend you to take a look at the following documentation, for you to check which one would better fit your present situation.

In these other two below articles, there are more examples, including code samples, of how to implement that on your application, that I would recommend you to check.

With these articles, you should be able to achieve your writing data functions and methods with no issue.

Let me know if the information helped you!

2 Comments

i will try this and give u a response asap. Thank you.
I followed your directions, the page actually talks about functions for androids. What i intentionally want to do is, i run the function, it writes those values into my Firestore db. It didn't.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.