Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

behavior differences between firebase realtime database and firestore

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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 });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading