How to update a single firebase firestore document

Jim Jones picture Jim Jones · Apr 6, 2018 · Viewed 71.3k times · Source

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

Answer

Juan Lara picture Juan Lara · Apr 6, 2018

You can precisely do as follows (https://firebase.google.com/docs/reference/js/firebase.firestore.DocumentSnapshot#ref):

var db = firebase.firestore();

db.collection("users").doc(doc.id).update({foo: "bar"});