Creating a document in firestore using cloud functions

16,883

Solution 1

admin.firestore() returns an instance of a Firestore object. As you can see from the API docs, the Firestore class doesn't have a ref() method. You're probably confusing it with the Realtime Database API.

Firestore requires you to organize documents within collections. To reach into a document, you could do this:

const doc = admin.firestore().doc(`/userProfile/${event.data.uid}`)

Here, doc is a DocumentReference. You can then set the contents of that document like this:

doc.set({ email: event.data.email })

Be sure to read the Firestore documentation to understand how to set up Firestore - there are many places where it's different than Realtime Database.

Solution 2

Here is my code. When I will create the new user below function will run.


    const functions = require('firebase-functions');
    const admin = require('firebase-admin');
    admin.initializeApp();


    exports.createProfile = functions.auth.user().onCreate((user) => {

      var userObject = {
         displayName : user.displayName,
         email : user.email,
      };
  
      return admin.firestore().doc('users/'+user.uid).set(userObject);
     // or admin.firestore().doc('users').add(userObject); for auto generated ID 
 
    });

Share:
16,883

Related videos on Youtube

Reshaud Ally
Author by

Reshaud Ally

Updated on June 15, 2022

Comments

  • Reshaud Ally
    Reshaud Ally about 2 years

    After authenticating a user in my app i want to create a cloud functions that creates a user profile document for them in my firestore userProfile collection.

    This is my entire index.js file for the cloud function

    // The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
    const functions = require('firebase-functions');
    
    // The Firebase Admin SDK to access the Firebase Realtime Database. 
    const admin = require('firebase-admin');
    admin.initializeApp(functions.config().firebase);
    
    //function that triggers on user creation
    //this function will create a user profile in firestore database
    exports.createProfile = functions.auth.user().onCreate(event => {
        // Do something after a new user account is created
        return admin.firestore().ref(`/userProfile/${event.data.uid}`).set({
            email: event.data.email
        });
    });
    

    Here is the error i am receiving

    TypeError: admin.firestore(...).ref is not a function
        at exports.createProfile.functions.auth.user.onCreate.event (/user_code/index.js:13:30)
        at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:59:27)
        at next (native)
        at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
        at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
        at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:53:36)
        at /var/tmp/worker/worker.js:695:26
        at process._tickDomainCallback (internal/process/next_tick.js:135:7)
    

    In the firestore cloud database I have a collection called userProfile where a document should be created with the unique id given to a user after authentication

    • Doug Stevenson
      Doug Stevenson over 6 years
      Looks good, run with it.
    • Reshaud Ally
      Reshaud Ally over 6 years
      when i try that i get this error admin.firestore().ref is not a function
    • Doug Stevenson
      Doug Stevenson over 6 years
      Could you show your entire file of code and the entire error?
    • Reshaud Ally
      Reshaud Ally over 6 years
      Added the entire error and file of code
  • Ryan Loggerythm
    Ryan Loggerythm about 3 years
    really clean code, thanks! I wish the official Firebase docs were as easy to read as your code