Shubham Bhakuni
Shubham Bhakuni

Reputation: 91

How to use SetOptions.merge with .set() when writing to Firestore in Cloud Functions

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

Upvotes: 3

Views: 3044

Answers (1)

Shubham Bhakuni
Shubham Bhakuni

Reputation: 91

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})

Upvotes: 6

Related Questions