Firebase Firestore函数从通配符获取数据,然后在另一个集合

时间:2018-01-14 12:30:12

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

我正在尝试从通配符中获取数据,然后更新数据库中的通知集合。我只是想知道是否有办法从通配符获取root doc数据并更新消息集合。

exports.likedByWho = functions.firestore.document('Posts/{postID}/{likeCollection}/{userWhoLikedID}')
    .onWrite((event) => { 
        console.log(event.params.postID);
        ^^^^^works
        console.log(event.params.likeCollection);
        ^^^^^works
        console.log(event.params.userWhoLikedID);
        ^^^^^works
        console.log(event.data.data());
        ^^^^^works but gives data of userWhoLikedID doc
        //console.log(`${event.params.feedID.data().uploadedBy}`);
         ^^^^^^^^^^^^^^^ Get user id of the person who posted this post.
         ^^^^^^^^^^^^^^^^^How to access root doc area.        


admin.firestore().collection("Posts").doc(`${event.params.postID}`).get().then(docData => console.log(docData.data()));
    ^^^^^^^^^^^^^is there another way of doing this using event.ref maybe


        return;
    });

要写入邮件集合,我可以使用event.refadmin.firestore()

1 个答案:

答案 0 :(得分:1)

您可以使用管理员SDK获取该文档的数据:

exports.likedByWho = functions.firestore.document('Posts/{postID}/{likeCollection}/{userWhoLikedID}')
    .onWrite((event) => { 
        console.log(event.params.postID);
        ^^^^^works
        console.log(event.params.likeCollection);
        ^^^^^works
        console.log(event.params.userWhoLikedID);
        ^^^^^works
        console.log(event.data.data());
        ^^^^^works but gives data of userWhoLikedID doc
        //console.log(`${event.params.feedID.data().uploadedBy}`);
         ^^^^^^^^^^^^^^^ Get user id of the person who posted this post.
         ^^^^^^^^^^^^^^^^^How to access root doc area.        

         var postRef = admin.firestore().collection('Posts')
                            .doc(event.params.postID);
         postRef.get().then(function(doc){
            console.log(doc.data());
            //data of the doc
         });

        return;
    });