Cloud Firestore - Cloud Functions:TypeError: collectionRef.child is not a function
I'm using Cloud Functions to update the comments number in the parent collection of Cloud Firestore, so when a comment added the Cloud Functions can automatically update the comment number.
Cloud Firestore - Cloud Functions:TypeError: collectionRef.child is not a function at exports.updateCommentNumbers1.functions.firestore.document.onCreate (/user_code/index.js:23:33) at Object. (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27) at next (native) at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36) at /var/tmp/worker/worker.js:716:24 at process._tickDomainCallback (internal/process/next_tick.js:135:7)
the code is:
exports.updateCommentNumbers1 = functions.firestore
.document('postlist/{postlistId}/comments/{commentsID}')
.onCreate((change, context) =>
{
const collectionRef = change.ref.parent;
const countRef = collectionRef.child('comment_number');
let increment;
if (change.after.exists() )
{
increment = 1;
}
else
{
return null;
}
return countRef.transaction((current) =>
{
return (current || 0) + increment;
}).then(() =>
{
return console.log('Comments numbers updated.');
});
});
The question is
How can I refer a parent collection through its subcollection?
How can I get the parent collection's field value and update it?
Thank you.