我试图从通配符获取数据,然后更新数据库中的通知集合。我只是想知道是否还有办法从通配符中获取根文档数据并更新消息集合。

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()

最佳答案

您可以使用admin 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;
    });

关于node.js - Firebase Firestore函数从通配符获取数据,然后将数据写入另一个集合中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48249598/

10-10 00:21
查看更多