繁体   English   中英

Firebase firestore:如何查询相关文档,然后更新每个文档

[英]Firebase firestore: how to query relevant documents and then update each of them

我正在 Firebase/firestore 上开发一个 web 应用程序,用户可以在其中登录并撰写自己的帖子。 数据以下列方式存储:

- 用户信息存储在 collection('user').doc('uid') 下。

-关于用户写的帖子的信息存储在collection('post').doc('postid')中,doc有'userinfo'和'uid'字段。 'userinfo' 字段包含存储在 'uid' 文档中的内容的精确副本,格式为 object。

以下是我想要执行的操作:

  1. 当用户更改数据时,更改会反映在文档中。

  2. 根据“uid”数据查找用户写过的所有帖子,然后更新这些数据中的用户信息。

最后一部分对我来说很棘手。 Firebase 文档涵盖了引用几乎是 static 的情况,即您知道写入/更新的确切路径。 我想做的是寻找一组不一定是 static 的文档,然后更新每个文档。

这是我为这项工作编写的代码。 第一部分工作没有任何问题。 当然,第二部分不起作用。 :) 执行第二部分的代码是什么?

 const update = () => {
             //This part is for updating user information. This works without any problem.
             firebase.firestore().collection('user').doc(user.uid).update({
                 username: username1,
                 nickname: nickname1,
                 intro: intro1 
             })
            .then(()=>{
             //This part is for updating all of the document that the user has written based on 'uid' value. This doesn't work. 
             //Below code is probably way off, but it shows where I am going and what I am trying to do.
             firebase.firestore().collection('post').where('uid','==',user.uid).get()
                 .then((querysnapshot)=>{
                     querysnapshot.forEach((doc)=>{
                         let ref=firebase.firestore().collection('post').doc(doc.id);
                             ref.update({
                                 userinfo: {nickname:nickname1,username:username1,intro:intro1}
                             })
                        })
                     })
                }).then(()=>{
                    alert("Successfully updated!");
                    window.location.href='/'+username1;
                }).catch((error)=>{
                    alert("Error!"); 
                })
}

非常感谢!

你运行这段代码的错误是什么? 这对我来说似乎是正确的。

但尽管如此,这里还是有一些处理此类更新的建议:

  • 不要在客户端执行第二部分,在服务器端使用 Firestore 触发器执行(在您的情况下在用户集合中创建一个 onUpdate 触发器): https://firebase.google.com/docs/functions/ firestore 事件

    • 在客户端做的问题,是因为如果用户关闭页面/浏览器或网站在更新过程中离线,您将有不一致的数据。
  • 获取查询结果后无需重新创建 DocumentReference,返回的文档已经有一个.ref,您可以直接调用 .ref.update()。

编辑:如果您想保留原始代码(在客户端更新),在所有更新结束之前发生的导航问题是因为 ref.update() 返回 promise。

因此,当客户端导航离开时,更新队列是异步在数据库上执行的。

为了解决这个问题,我会使用 Promise.all() 来等待所有更新完成。

firebase.firestore().collection('post').where('uid','==',user.uid).get()
   .then((querysnapshot)=>{
       const promises = [];
       querysnapshot.forEach((doc)=>{
           promises.push(doc.ref.update({
               userinfo: {nickname:nickname1,username:username1,intro:intro1}
           });
       });
       Promise.all(promises).then(()=>{window.location.href='/'+username1;});
   });

或者使用 await 语法(我认为这样更容易维护和理解):

const querysnapshot = await firebase.firestore().collection('post').where('uid','==',user.uid).get();

const promises = [];
querysnapshot.forEach((doc)=>{
   promises.push(doc.ref.update({
      userinfo: {nickname:nickname1,username:username1,intro:intro1}
   });
});
await Promise.all(promises);
window.location.href='/'+username1;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM