Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops `querysnapshot.forEach`方法不';迭代_Loops_Google Cloud Firestore_Google Cloud Functions - Fatal编程技术网

Loops `querysnapshot.forEach`方法不';迭代

Loops `querysnapshot.forEach`方法不';迭代,loops,google-cloud-firestore,google-cloud-functions,Loops,Google Cloud Firestore,Google Cloud Functions,我正试图在从查询中获得的每个文档下面添加一个集合notifs。我从查询中获得了document1、document2和document3,因此我想在所有这些文档下添加notifs集合,但结果是notifs集合仅添加到document1下。其他2个notifs集合不是添加到document2或document3下,而是从根目录添加的,以斜体显示其路径。似乎querysnapshot.forEach(doc=>方法不会迭代。如何在来自查询的所有文档下添加notifs集合,而不仅仅是1? 谢谢。 这是

我正试图在从查询中获得的每个文档下面添加一个集合
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)
    }
 })

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

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

我所看到的是,您正在混合
then()
async/await
,这是不推荐的

您应该尝试使用以下改编代码,并从空的
crews
集合开始。如果它不起作用,我将删除此答案


从您的完整代码来看,不清楚为什么您会在控制台中的
crews
集合中显示两次相同的文档ID

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

我所看到的是,您正在混合
then()
async/await
,这是不推荐的

您应该尝试使用以下改编代码,并从空的
crews
集合开始。如果它不起作用,我将删除此答案


看起来很奇怪;你能找出这两种文档类型(添加的集合与未添加的集合)之间的区别吗?比如它在树中是否有空文档?谢谢,穆图。这很奇怪,不是吗?但是所有这些文档都有相同的firestore结构。为什么不
querysnapshot.foreach(doc=>
方法适用于所有
querysnapshot.docs
,但仅适用于1个doc?您不会显示云函数的完整代码,但如果Cf仅包含问题中显示的代码(并且是后台触发的Cf),则应返回承诺链:
return query.get()。然后(querysnapshot=>{…})。catch(…)
。另外,什么是
查询
以及您的CF是如何触发的?我认为您的CF在两个不同的级别创建了两次集合…请共享整个代码。@Renaudtarnec。谢谢Renaud,我发布了我的整个代码。请检查并给出您的评论。看起来很奇怪;您能找出它们之间的区别吗两种文档类型(已添加集合与未添加集合)?如树中是否有空文档?谢谢,Muthu。这很奇怪,不是吗?但所有这些文档都具有相同的firestore结构。为什么
querysnapshot.foreach(doc=>
方法适用于所有
querysnapshot.docs
,但仅适用于1个doc?您不会显示云函数的完整代码,但如果Cf仅包含问题中显示的代码(并且是后台触发的Cf),则应返回承诺链:
return query.get()。然后(querysnapshot=>{…})。catch(…)
。还有,什么是
查询
以及您的CF是如何触发的?我认为您的CF在两个不同的级别创建了两次集合…请共享整个代码。@Renaudtarnec。谢谢Renaud,我发布了我的全部代码。请检查并给我您的评论。
@Renaud
谢谢您的帮助,Renaud。
@Renaud
谢谢你的帮助,雷诺。
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;
        }

});