I have this architecture in my firestore: test1/{test1ID}/test2/
I want to interact with my firestore in cloud functions whenever new data is added to test1ID.
My goal is to update the test1ID document where data is updated, with the value of [0]th + [1]th + [2]th document under subcollection test2.
But with the code below I get test2 document list(currentTest2DocumentList) undefined.
(error message => TypeError: Cannot read property '0' of undefined)
How can I get a valid list of documents under subcollection?
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.onItemDocumentUpdate = functions.firestore.document('test1/{test1ID}')
.onUpdate(async (change, context) => {
const currentTest1ID = context.params.test1ID
const currentTest2DocumentList = await
admin.firestore().collection('test1').doc(currentTest1ID).collection('test2').get()
.then(function(docs) {return docs.forEach(function (doc){doc.data()})})
const addedValue = currentTest2DocumentList[0].field1 + currentTest2DocumentList[1].field1 +
currentTest2DocumentList[2].field1
return admin.firestore().collection('test1').doc(currentTest1ID).update({testField:
addedValue});