【发布时间】:2021-01-18 10:40:11
【问题描述】:
我正在尝试在从查询中获得的每个文档下方添加一个集合 notifs。我从查询中得到了document1、document2和document3,所以我想在所有这些文档下添加notifs集合,但结果是notifs集合只添加在document1下。其他 2 个notifs 集合不是在 document2 或 document3 下添加的,而是从根目录添加的,以斜体显示其路径。 querysnapshot.forEach(doc => 方法似乎没有迭代。如何在来自查询的所有文档中添加 notifs 集合,而不仅仅是 1?
谢谢。
这是我的代码;
const functions = require('firebase-functions');
const admin = require('firebase-admin');
var GeoFirestore = require('geofirestore').GeoFirestore;
admin.initializeApp();
const firestore = admin.firestore();
const geofirestore = new GeoFirestore(firestore);
exports.sendNotification = functions.firestore
.document("crews/{crew}/clients/{client}")
.onCreate(async snapshot => {
try {
const clientfield = snapshot.data().field;
const clientgeopoint = snapshot.data().g.geopoint;
const geocollection = geofirestore.collectionGroup('pros');
const query = geocollection.near({center: clientgeopoint, radius: 10}).where('field', '==', clientfield);
const promises = [];
const querySnapshot = await query.get();
querySnapshot.forEach(doc => {
const docId = doc.id;
const uid = doc.data().uid;
const geonotifs = firestore.collection('crews').doc(uid).collection('pros').doc(docId);
promises.push(geonotifs.ref.collection('notifs').add({
'field': clientfield,
'geopoint': clientgeopoint}));
});
return Promise.all(promises);
} catch (error) {
console.log(error)
}
})
【问题讨论】:
-
看起来很奇怪;您能找出这两个文档品种(添加集合与未添加集合)之间的区别吗?比如树中是否有空文档?
-
谢谢,穆图。这很奇怪,不是吗?但所有这些文件都具有相同的 Firestore 结构。为什么
querysnapshot.foreach(doc =>方法不适用于所有querysnapshot.docs,而只适用于1 个文档? -
您没有显示云函数的完整代码,但如果 Cf 仅包含您的问题中显示的代码(并且是背景触发的 CF),您应该返回承诺链:@987654332 @。另外,
query是什么以及您的 CF 是如何触发的?我认为您的 CF 在两个不同级别创建了一些集合两次...请分享整个代码。 -
@Renaudtarnec。谢谢雷诺,我发布了我的整个代码。请检查并给我你的cmets。
标签: loops google-cloud-firestore google-cloud-functions