firebase.firestore.Document.onCreate - Reference.set failed: First argument contains undefined

711 views
Skip to first unread message

Jorge Willian Alvarado Jiménez

unread,
Apr 18, 2019, 9:58:54 AM4/18/19
to Firebase Google Group

I have created a firebase function that listen on onCreate event, however the DocumentSnapshot.data() is returning empty.

The function code is:


exports.createClientAccount = functions.firestore
 
.document('/userProfile/{userId}/clientList/{clientId}')
 
.onCreate(async (snap, context) => {
    console
.log('****snap.data(): ', snap.data()); //Showing Empty from the console.
   
return admin
     
.auth()
     
.createUser({
        uid
: context.params.clientId,
        email
: snap.data().email,
        password
: '123456789',
        displayName
: snap.data().fullName,
     
})
     
.then(userRecord => {
       
return admin
         
.database()
         
.ref(`/userProfile/${userRecord.uid}`)
         
.set({
            fullName
: userRecord.displayName, //ERROR here: Reference.set failed: First argument contains undefined
            email
: userRecord.email,
            coachId
: context.params.userId,
            admin
: false,
            startingWeight
: snap.data().startingWeight,
         
});
     
})
     
.catch(error => {
        console
.error('****Error creating new user',error);
     
});
 
});

The document IS created on the firebase database under


byaAd.png


R0gck.png

/userProfile/{userId}/clientList/{clientId}


As per the documentation, onCreate listens when a new document is created and returns the snapshot of the data created through the DocumentSnapshot interface. However, I have checked from the console that snap.data() is empty. I don't understand why it is empty if the document is created successfully on the database.

From the function code, return admin.auth.createUser({}) is creating the user as anonymous because snap.data().email is undefined, but it should create a non anonymous user.



Kato Richardson

unread,
Apr 19, 2019, 1:34:09 PM4/19/19
to Firebase Google Group
Hi Jorge,

You haven't included your createUser() method, nor the process showing how the data is getting created. If you can encapsulate this and turn it into a smallish repro, I'd be happy to try it out and see if I can produce the same results.

Without further context, it looks like the doc is getting created separate from the fields being written--based simply on the log output you shared, which shows that snap.data() is a completely empty object.

☼, Kato

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/cc9c2b81-86b2-4378-b459-2152e6daf38a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Jorge Willian Alvarado Jiménez

unread,
Apr 19, 2019, 3:35:47 PM4/19/19
to Firebase Google Group
Hi Kato, I was able to resolve this. Problem was that when I invoke the function for creating the document:

firestore.collection.add({})

I was calling it without any field parameters.

I modified the code so the document is added with the fields, so the when the cloud function is triggered the DocumentSnapshot.data() is not empty.

  async clientCreate(
    fullName
: string,
    email
: string,
    startingWeight
: number
 
): Promise<any> {
   
const newClientRef = await this.firestore
     
.collection(`userProfile/${this.userId}/clientList/`)
     
.add({
        fullName
, //I added the fields inside the .add
        email
, //
        startingWeight
: startingWeight * 1, //
     
});
   
return newClientRef.update({
      id
: newClientRef.id
   
});
 
}

Previous version of the code was creating the document reference only by invoking .add({}) and immediately I called the .update({}) but the cloud function gets triggered after .add({}) so it never was able to grab the recently updated fields, so DocumentSnapshot.data() was empty. 
To unsubscribe from this group and stop receiving emails from it, send an email to fireba...@googlegroups.com.

To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/cc9c2b81-86b2-4378-b459-2152e6daf38a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kato Richardson

unread,
Apr 22, 2019, 3:26:28 PM4/22/19
to Firebase Google Group
Nice! Glad that (lucky) guess was helpful.

☼, Kato

To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.

To post to this group, send email to fireba...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages