![](/img/trans.png)
[英]Firestore - Get document with ID whose path is unknown - Firebase Cloud Functions
[英]Cloud Functions get list from firestore path
我的 firestore 中有這個架構:test1/{test1ID}/test2/
每當將新數據添加到 test1ID 時,我都想在雲功能中與我的 firestore 進行交互。
我的目標是更新數據更新的 test1ID 文檔,值為子集合 test2 下的 [0]th + [1]th + [2]th 文檔。
但是使用下面的代碼我得到了未定義的 test2 文檔列表(currentTest2DocumentList)。
(錯誤消息 => TypeError:無法讀取未定義的屬性“0”)
如何獲得子集合下的有效文檔列表?
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});
您似乎沒有正確使用 forEach。 試試這個:
const currentTest2DocumentList = await
admin.firestore().collection('test1').doc(currentTest1ID).collection('test2').get()
.then(function(docs) {
let res = []
docs.forEach(doc => res.push(doc.data()));
return res;
})
或者更好:
const currentTest2DocumentList = await
admin.firestore().collection('test1').doc(currentTest1ID).collection('test2').get()
.then(docs => docs.map(doc => doc.data()));
test2
集合中是否有 3 個以上的文檔? 因為那樣的話,閱讀所有這些內容似乎是一種浪費。 您可以添加limit(3)
來解決這個問題
const currentTest2DocumentList = await
admin.firestore().collection('test1').doc(currentTest1ID).collection('test2').limit(3).get()
.then(docs => docs.map(doc => doc.data()));
這行代碼沒有達到您的預期:
const currentTest1ID = context.params.ID
我建議記錄currentTest1ID
。 它將是不確定的。 這是因為context.params
只包含在文檔路徑中用通配符標記的鍵。 您沒有任何名為“ID”的通配符。 也許這就是你想要的:
const currentTest1ID = context.params.test1ID
請注意, test1ID
與您在 function 路徑中定義的通配符匹配。
它返回未定義的原因是 forEach function 不返回任何值。 嘗試訪問集合快照的文檔字段並使用 javascript map function 像這樣:
// IMPORT
const functions = require('firebase-functions');
const admin = require('firebase-admin');
// INIT
admin.initializeApp();
// LISTEN TO UPDATES
exports.onItemDocumentUpdate = functions.firestore.document('test1/{test1ID}').onUpdate(async (change, context) => {
// GETS PARAMS
const currentTest1ID = context.params.test1ID
// READ THE COLLECTION
const currentTest2DocumentList = await admin.firestore().collection('test1/' + currentTest1ID + '/test2').get().then(function(collectionSnapShot) {
return collectionSnapShot.docs.map(function (doc){
return doc.data();
})
})
// GETS THE SUM USING THE REDUCE METHOD.
const addedValue = currentTest2DocumentList.reduce((x,y)=>{
return x + y.field1;
}, 0);
// UPDATES TEST.
return admin.firestore().collection('test1').doc(currentTest1ID).update({testField:
addedValue
});
})
我還可以建議在我的答案中使用遞增和遞減解決方案來減少文檔閱讀嗎? 它將為您節省很多每月的賬單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.