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

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

问题描述

认证后,我试图在/users/查找用户文档,然后我想用来自 auth 对象的数据以及一些自定义用户属性更新文档.但是我收到一个错误,提示更新方法不存在.有没有办法更新单个文档?所有 firestore doc 示例都假定您拥有实际的 doc 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"})
      });
 })

推荐答案

你可以精确的做如下(https://firebase.google.com/docs/reference/js/v8/firebase.firestore.DocumentReference):

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

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

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