2
votes

I have a cloud function that creates a Firestore entry when a new user is registered

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

  var userObject = {
     displayName : user.displayName,
     picUrl:user.photoURL,
     authType:user.providerData,
     email : user.email,
     newUser: true,
     bookings:0
  };

  console.log("Merge:true")
  return admin.firestore().doc('users/'+user.uid).set(userObject, SetOptions.merge())
  .then((response)=>{
    console.log("New user result: ",response)
    return response;
  }).catch((err)=>
  {
    console.log("New user Error: "+err)
    return err;
  });
});

I want to use setoptions.merge() but It is not working, I want to update because I am also writing the device token on firestore for FCM messages.

NewUser ReferenceError: SetOptions is not defined at exports.newUser.functions.auth.user.onCreate (/srv/index.js:26:67) at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23) at /worker/worker.js:825:24 at at

I refered the google cloud documentation where they said to pass it as {merge:true} but that also didn't help. I refered this post on stackoverflow Why is my Cloud Firestore Transaction not merging when using SetOptions.merge()? But this also didn't work. Please help me as I don't know much about firestore cloud functions

1
What you are doing now won't work. The documentation is pretty clear that you are supposed to pass {merge:true}. What's wrong with that?Doug Stevenson
The problem was with user.providerData which return as json object with a function which was causing the encoding error. as for merge options, it is .set(data,{merge:true})Shubham Bhakuni
If you are saying that there's no longer a problem, please delete the question or simply answer your own question.Doug Stevenson
Could you confirm that you have resolved the issue?Enrique Del Valle
Yes @EnriqueDelValle my problem was solved.Shubham Bhakuni

1 Answers

2
votes

The problem was with user.providerData which return as json object with a function which was causing the encoding error. As for merge options, it is .set(data,{merge:true})