使用geofirestore,我可以在云函数中从集合users/{userId}/pros中查询并获得结果文档(Doc)。现在,我想在来自查询的每个文档users/{userId}/pros/{proId}/notifs下添加一个新的集合users/{userId}/pros/{proId}。所以,我这样写;
exports.sendNotification = functions.firestore
.document("users/{user}/consumers/{consumer}")
.onCreate(async snapshot => {
try {
query.get().then(querySnapshot => {
querySnapshot.forEach(doc => {
await doc.ref.collection('notifs').add({ . . .});
}).catch ((error) =>
console.log(error));然而,我总是收到错误,TypeError: Cannot read property 'collection' of undefined。好像出了什么问题?Geofiretore queryDocumentSnapshot似乎没有collection()属性。谢谢你的帮助。
发布于 2021-01-27 12:15:38
在docs循环之前缺少forEach属性。根据QuerySnapshot 文档的说法,这个属性是:
QuerySnapshot中所有文档的数组
因此,如果您想遍历文档,那么应该是:
...
query.get().then(querySnapshot => {
querySnapshot.docs.forEach(doc => {
...根据我的测试,这将至少直接工作在节点(这也是用于云功能)。
https://stackoverflow.com/questions/65718669
复制相似问题