如何更新单个Firebase Firestore文档 [英] How to update a single firebase firestore document

查看:92
本文介绍了如何更新单个Firebase Firestore文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过身份验证后,我尝试在/users/中查找用户文档,然后使用来自auth对象以及一些自定义用户属性的数据更新该文档.但是我收到一个错误消息,说更新方法不存在.有没有更新单个文档的方法?所有的firestore文档示例均假定您具有实际的文档ID,并且没有任何使用where子句查询的示例.

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

推荐答案

您可以精确地执行以下操作(

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

这篇关于如何更新单个Firebase Firestore文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆