【问题标题】:Update data using firestore cloud function使用 Firestore 云功能更新数据
【发布时间】:2020-08-02 21:22:24
【问题描述】:

我需要帮助,我正在尝试使用云功能更新我的 cmets 集合,但我的代码似乎不起作用。我的函数成功运行,但当我的 userPhotoUrl 更新时没有更新我的 avatarUrl

这里是我要更新的集合的整个路径:“/cmets/{postId}/cmets/{commentId}”

my firestore collection

 exports.onUpdateUser2 = functions.firestore
    .document("/users/{userId}")
    .onUpdate(async (change, context) => {
      const userUpdate = change.after.data();
      const userId = context.params.userId;
      const newPhotoUrl = userUpdate.photoUrl;
      console.log("userId",userId);
      console.log("newPhotoUrl",newPhotoUrl);

    const querySnapshot = await admin.firestore().collection("comments").get();
    querySnapshot.forEach(doc => {
      console.log("doc",doc.data());
      const postId = doc.id;
      const comments = admin.firestore().collection("comments").doc(postId).collection("comments").where("userId","==",userId).get();
      comments.forEach(doc2 => {
        return doc2.ref.update({avatarUrl: newPhotoUrl});
      });
    });
 });

谢谢,

更新

我尝试更改代码,使用 then 来处理这些不同的承诺,但我真的不知道为什么 cmetsRef.get() 似乎返回了空的 querySnapshots,因为 cmets 集合在我的 firestore 数据库中有多个文档,其中每个文档中都有另一个 cmets 集合,在这一秒内 cmets 集合中有一堆包含数据的文档。有了这整个路径,我不知道如何迭代,直到进入包含我需要更新的数据的文档。有人可以帮我吗?

exports.onUpdateUserUpdateComments = functions.firestore
        .document("/users/{userId}")
        .onUpdate(async (change, context) => {
          const userUpdate = change.after.data();
          const userId = context.params.userId;
          const newPhotoUrl = userUpdate.photoUrl;
          console.log("userId",userId);
          console.log("newPhotoUrl",newPhotoUrl);
          const commentsRef= admin.firestore().collection("comments");
          return commentsRef.get().then(querySnapshot => {
            return querySnapshot.forEach(doc => {
              return admin
                 .firestore()
                 .collection("comments")
                 .doc(postId)
                 .collection("comments")
                 .where("userId", "==", userId)
                 .get()
                 .then(doc => {
                   if (doc.exists) {
                     doc.ref.update({avatarUrl: newPhotoUrl});
                   }
                   return console.log("The function has been run.");
                 });
            });
          });
     });

【问题讨论】:

  • 您的代码没有返回一个在所有异步工作完成后解析的承诺。从对get() 的调用和对update() 的所有调用中,您只是没有正确处理所有这些不同的承诺。如果您是 JavaScript 新手,我会说 Cloud Functions 不是开始学习的最佳场所。从简单的事情开始,然后逐步增加复杂性。
  • 是的,我是 Java Script 的新手,但我确实需要先使用 Cloud Functions,所以你能帮我吗?我尝试通过使用then 来更改此代码
  • 我不确定我是否能理解您的用例,您的数据库结构看起来像 /comments/{postId}/comments/{commentId},尽管您的函数正在侦听 /users/{userId} 上的更新。命名可能会有些混乱。您是否尝试更新给定 postcomments/ 子集合中的单个文档?

标签: javascript node.js google-cloud-firestore google-cloud-functions


【解决方案1】:

不用尝试,应该是这样的:

return admin.firestore().collection("comments")
    .doc(postId)
    .where("userId", "==", userId)
    .get()
    .then(doc => {
        if (doc.exists) {
            doc.ref.update({avatarUrl: newPhotoUrl});
        }
        return console.log("The function has been run.");
    });

不管怎样,听从 Doug Stevenson 的建议,你不应该开始在 Cloud Functions 中学习 JS,因为那些嵌套循环有点奇怪,你可能缺乏一个好的学习起点。

【讨论】:

  • 嗨,我像你一样尝试,但它不起作用,因为我正在尝试做的是在具有多个文档的 cmets 集合中迭代,其中每个文档都有另一个集合,也称为 cmets 和在本文档中的哪里有我要更新的数据。
  • 所以这就是我做嵌套循环的原因,因为我没有找到另一种方法来迭代集合集合,你知道怎么做吗?
  • 查看下面的 documentation,它解释了 Cloud Firestore 服务器客户端库的 .listCollections() 方法用于列出文档引用的所有子集合。请注意,无法使用移动/网络客户端库检索收藏列表
  • @sllopis 它不起作用,因为我需要先获取文档,但我无法获取它...
  • 请更新您的问题以添加您的数据库结构@JeremyDormevil,以便我们为您提供帮助。您可以从数据库中获取单个文档吗?如果是这样,下一步是您能否在集合中获取满足特定条件的一系列文档?如果是这样,您可能需要查看对您的用例有用的集合组查询。
猜你喜欢
  • 2019-08-07
  • 2019-12-13
  • 1970-01-01
  • 2018-06-30
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多