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...
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 ofcommunityID. In other words can you verify, outside of the Cloud Function, that this query works correctly. - Renaud Tarneccommunity.get('communityID')is not undefined? - Renaud Tarneccommunity.get('communityID')is never undefined in my cloud function. - Ashton Cofer