I'm attempting to retrieve all the documents from each child collection (ratings) and update the average rating in the foobar document. However, I am encountering an error in one of my callable functions:
Unhandled error RangeError: Maximum call stack size exceeded
at isLength (/srv/node_modules/lodash/lodash.js:11713:22)
at isArrayLike (/srv/node_modules/lodash/lodash.js:11333:31)
at keys (/srv/node_modules/lodash/lodash.js:13307:14)
at /srv/node_modules/lodash/lodash.js:4900:21
at baseForOwn (/srv/node_modules/lodash/lodash.js:2990:24)
at Function.mapValues (/srv/node_modules/lodash/lodash.js:13400:7)
at encode (/srv/node_modules/firebase-functions/lib/providers/https.js:183:18)
at /srv/node_modules/lodash/lodash.js:13401:38
at /srv/node_modules/lodash/lodash.js:4905:15
at baseForOwn (/srv/node_modules/lodash/lodash.js:2990:24)
Code:
const fooBarQuerySnapshot = await db
.collection("foobars")
.orderBy('ratingCount', 'desc')
.get();
const fooBars = fooBarQuerySnapshot.docs
.map(async (x: QueryDocumentSnapshot<DocumentData>) => {
// fetch foo bar data
const data = x.data();
// get sub collection of ratings
const ratingQuerySnapshot = await db
.collection(collectionName)
.doc(x.id)
.collection('ratings')
.get();
// calculate average rating from the ratings subcollection
data.avgRating = ratingQuerySnapshot.docs
.map((y: QueryDocumentSnapshot<DocumentData>) => y.data().rating)
.reduce((p, c) => p + c, 0) / data.ratingCount;
return data;
});