i used to use realtime database on firebase and i always used update which used to create if the node is missing. So for the below code:
async updateUser(uid: string, payload: any) {
const userDocRef = doc(this.firestore, 'users/' + uid );
return updateDoc(userDocRef, {"email": "xxx@moblize.it"});
}
Realtime db will simply create a new node with uid if does not exist or else update. However, doing the same with Firestore db it gives me error as
api.js?onload=__iframefcb905117:29 ERROR Error: Uncaught (in promise): FirebaseError: [code=not-found]: No document to update: projects/baniya-38d78/databases/(default)/documents/users/vdZMeCkTfrhd12j1CWyR9eGhra12
FirebaseError: No document to update: projects/baniya-38d78/databases/(default)/documents/users/vdZMeCkTfrhd12j1CWyR9eGhra12
is that how firestore db works and i have to always check if it exist to decide create or update?
>Solution :
To perform a create-or-update operation on Firestore, use set with merge options as shown in the second code sample in the documentation on setting a document. In your case that’d be:
setDoc(userDocRef, {"email": "xxx@moblize.it"}, { merge: true });