【问题标题】:`querysnapshot.forEach` method doesn't iterate`querysnapshot.forEach` 方法不迭代
【发布时间】: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


【解决方案1】:

从您的完整代码中,不清楚为什么您在控制台中的 crews 集合中拥有 两次 相同的文档 ID。

一方面,我们看到 ID fPrxMd.... 是斜体,这意味着该文档不存在于 crews 集合中,但它是子子集合路径的一部分(有关更多详细信息,请参阅 here ),另一方面,我们看到一个具有相同 ID 的“正版”文档。这很奇怪......也许这些是按特定顺序创建的,会产生这种情况,但同样,从你的完整代码来看,我不清楚这是怎么发生的。可能其他 SO 成员也遇到过这种情况。

我看到的是你把then()和async/await混在一起了,不推荐。

您应该尝试使用以下修改后的代码,并从一个空的 crews 集合开始。 如果它不起作用,我会删除这个答案。

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: brewgeopoint, 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).collection('notifs');
                promises.push(geonotifs.add({
                    'field': clientfield, 'geopoint': clientgeopoint
                }));
            });

            return Promise.all(promises);
            
        } catch (error) {
            console.log(error);
            return null;
        }

});

【讨论】:

  • @Renaud 谢谢你的帮助,雷诺。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-23
  • 2015-01-20
  • 2016-02-16
  • 2016-05-14
  • 2018-09-12
  • 2017-03-12
  • 2023-03-06
相关资源
最近更新 更多