Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update a single firebase firestore document

After authenticating i'm trying to lookup a user document at /users/, then i'd like to update the document with data from auth object as well some custom user properties. But I'm getting an error that the update method doesn't exist. Is there a way to update a single document? All the firestore doc examples assume you have the actual doc id, and they don't have any examples querying with a where clause.

firebase.firestore().collection("users").where("uid", "==", payload.uid)   .get()   .then(function(querySnapshot) {       querySnapshot.forEach(function(doc) {           console.log(doc.id, " => ", doc.data());           doc.update({foo: "bar"})       });  }) 
like image 723
Jim Jones Avatar asked Apr 05 '18 22:04

Jim Jones


People also ask

How do I update firestore files?

Firestore Update Entire DocumentgetDatabase() → where we want to update a document. doc() → where we'll be passing references of database, collection and ID of a document that we want to update. setDoc() → where we actually pass new data that we want to replace along with the doc() method.

Can I update document ID in firebase?

There is no API to change the ID of an existing document, nor is there an API to move a document. If you want to store the same contents in a different document, you will have to: Read the document from its existing key. Write the document under its new key.

How do you update an array in firestore?

// Atomically remove a region from the "regions" array field. # Atomically add a new region to the 'regions' array field. # // Atomically remove a region from the 'regions' array field. # Atomically add a new region to the 'regions' array field.


1 Answers

You can precisely do as follows (https://firebase.google.com/docs/reference/js/v8/firebase.firestore.DocumentReference):

var db = firebase.firestore();  db.collection("users").doc(doc.id).update({foo: "bar"}); 
like image 172
Juan Lara Avatar answered Sep 25 '22 19:09

Juan Lara