geofirestore 查询文档快照

问题描述

使用 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() 属性。感谢您的帮助。

解决方法

docs 循环之前缺少 forEach 属性。根据 QuerySnapshot documentation 属性是:

QuerySnapshot 中所有文档的数组

所以如果你想遍历文档,它应该是:

...
query.get().then(querySnapshot => {
 querySnapshot.docs.forEach(doc => {
...

根据我的测试,这至少可以直接在节点中工作(也用于云功能)