【发布时间】:2021-08-16 23:07:33
【问题描述】:
我正在编写让用户更新他们的头像(又名 photoURL)的客户端逻辑。我需要做的是更新名为 comments 的子集合中的每个文档,该子集合存在于该用户的名为 posts 的集合下,并更新 photoURL 属性。
这是我需要访问的用户 cmets 子集合的示例路径:
/users/XrMD3azk4Jess5KNTSICv4RYEj02/posts/what-architecture-style-is-this_r3nosJHQIt/comments
我将如何遍历每个帖子文档并更新每个评论文档?
作为 FireStore 的新手,我如何引用集合的子集合,然后循环遍历它并更新每个评论文档?
如果有人可以帮我一把,我可以开始走上正确的道路,我将不胜感激。谢谢。
const commentsCol = this.$fire.firestore
.collection('users')
.doc(`${this.userProfile.uid}`)
.collection('posts')
// I need to loop through here and get all the post docs, right?
// ... then get the comments collection
.collection('comments')
const querySnapshot = await commentsCol.get()
querySnapshot.forEach((comment) => update the photoURL property here..)
【问题讨论】:
-
这应该是一个相当简单的三步过程:1)来自子集合的read all documents,2)循环结果,3)update each document。你有没有尝试过?
-
所以我有
const postsCol = this.$fire.firestore .collection('users') .doc(${this.userProfile.uid}) .collection('posts')但不知道如何访问 cmets 子集合 -
应该是
.collection("comments")。但是,如果您已经尝试过某些事情,那么编辑您的问题以包括在内会大大增加有人可以提供帮助的机会。 -
@FrankvanPuffelen 好的,弗兰克,我在这里挣扎……只是一个简单的问题,但我可能想多了?请参阅上面的更新代码...我放弃了。
标签: javascript firebase google-cloud-firestore