0
votes

The goal of my function is to loop through several 'community' documents in the collection 'communities'. Each community document has a collection of documents called 'posts' where I query the document with the highest value of 'hotScore'. I then loop through those documents (contained in postsQuerySnapArray) to access the data in them.

My issue is that when I loop through the postQuerySnapArray, every document in postQuerySnap is of type undefined. I have verified that all communities contain a 'posts' collection and every post document has a 'hotScore' property. Does anyone know what could be causing this behavior? Thanks!

exports.sendNotificationTrendingPost = functions.https.onRequest(async (req, res) => {
    try {

        const db = admin.firestore();
        const communitiesQuerySnap = await db.collection('communities').get();

        const communityPromises = [];

        communitiesQuerySnap.forEach((community) => {
            let communityID = community.get('communityID');
            communityPromises.push(db.collection('communities').doc(communityID).collection('posts').orderBy('hotScore', 'desc').limit(1).get())
        });

        const postsQuerySnapArray = await Promise.all(communityPromises);

        postsQuerySnapArray.forEach((postsQuerySnap, index) => {

            const hottestPost = postsQuerySnap[0]; //postsQuerySnap[0] is undefined!
            const postID = hottestPost.get('postID'); //Thus, an error is thrown when I call get on hottestPost
            //function continues...
1
Can you confirm that you get a result if you do db.collection('communities').doc(communityID).collection('posts').orderBy('hotScore', 'desc').limit(1).get() from a web page (or a screen of your app) with a correct value of communityID. In other words can you verify, outside of the Cloud Function, that this query works correctly. - Renaud Tarnec
Also, are you sure that, for each result of the first query, community.get('communityID') is not undefined? - Renaud Tarnec
Yes, I just confirmed that the query works correctly when run inside my iOS app. I programmed the exact functionality of my Firebase cloud function above (querying the hottestPost from every community) and it works as expected. I also verified that community.get('communityID') is never undefined in my cloud function. - Ashton Cofer
I also get the same behavior when I query without ordering by 'hotScore' or without a limit - Ashton Cofer

1 Answers

1
votes

Finally figured out what my problem was. Instead of getting the element in postsQuerySnap by calling

const hottestPost = postsQuerySnap[0];

I changed my code to get the element by using a forEach on postsQuerySnap

var hottestPost;
postsQuerySnap.forEach((post) => {
    hottestPost = post;
})

I'm still not quite sure why postsQuerySnap[0] didn't work originally, so if anyone knows please leave a comment!

Edit: As Renaud said in his comment, a better fix would be const hottestPost = postsQuerySnap.docs[0] since postsQuerySnap is not an array.