Kamil Kamili
Kamil Kamili

Reputation: 1927

Firebase Firestore Functions get data from wildcards and then write data in another collection

I am trying to get data from the wildcards and then update a notification collection in my database. I was just wondering if there is any way to get root doc data from the wildcards as well and to update the message collection.

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

To write in the message collection, can I use event.ref or admin.firestore().

Upvotes: 1

Views: 1107

Answers (1)

Rosário P. Fernandes
Rosário P. Fernandes

Reputation: 11326

You can get that document's data using the 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;
    });

Upvotes: 1

Related Questions