简体   繁体   English

使用 Firestore 云更新数据 function

[英]Update data using firestore cloud function

I need help, i'm trying to update my comments collections using cloud function, but my code doesn't seem to work.我需要帮助,我正在尝试使用云 function 更新我的评论 collections,但我的代码似乎不起作用。 My function succesfully run but doesn't update my avatarUrl when my userPhotoUrl is update我的 function 成功运行,但当我的 userPhotoUrl 更新时没有更新我的头像Url

Here the whole path of the collection that i want to update: "/comments/{postId}/comments/{commentId}"这里是我要更新的集合的整个路径:“/comments/{postId}/comments/{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});
      });
    });
 });

Thank you,谢谢,

UPDATE更新

I try to change the code, by using then to deal with these various promises but i don't really know why commentsRef.get() seem to return me empty querySnapshots, because the comments collections in my firestore database have multiple documents where in each documents there is a another comments collections where in this seconds comments collections there is a bunch of documents containing data.我尝试更改代码,通过使用then来处理这些不同的承诺,但我真的不知道为什么 commentsRef.get() 似乎返回了空的 querySnapshots,因为我的 firestore 数据库中的评论 collections 有多个文档,每个文档文档还有另一个评论 collections 在这几秒钟内评论 collections 有一堆包含数据的文档。 With this whole path i don't know how to iterate until being in the documents containing the data that i need to update.有了这整个路径,我不知道如何迭代,直到进入包含我需要更新的数据的文档。 Can someone help me please?有人能帮助我吗?

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

Without trying it, it should be something like this:不用尝试,它应该是这样的:

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

Regardless, following Doug Stevenson's advice, you shouldn't start learning JS in Cloud Functions, as those nested loops are a bit strange and you may lack a good starting point for learning.无论如何,按照 Doug Stevenson 的建议,你不应该开始在 Cloud Functions 中学习 JS,因为那些嵌套循环有点奇怪,你可能缺乏一个好的学习起点。

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

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