【问题标题】:Firestore query: getting data and storing it into an variable . How to use that variable in cloud functions?Firestore 查询:获取数据并将其存储到变量中。如何在云函数中使用该变量?
【发布时间】:2018-08-27 16:39:09
【问题描述】:

为了从 Firestore 中获取数据,我查询了一个文档并将该数据放入变量中。现在,我需要在代码的其他部分使用该变量。 当我使用该变量时,它没有得到任何数据。如何解决这些错误。

var d1;
                    var getdata = respond.get()
                                    .then(doc =>{
                                    if(!doc.exists){
                                    console.log('No such document');
                                    }else{
                                    console.log('Document data:', doc.data());
                                     d1 = doc.data();// In d1 I am  not getting the data of that document 
                                    }
                                    }).catch(err => {
                                    console.log('Error getting documnet', err);
                                    });

在 for 循环中,我使用的是 d1 变量。但它没有执行这些 for 循环

for(var k in d1){
                     var p = d1[k].PhoneNumber;
                     let rph = respond.where(receiverph ,"==", p)
                                    .set({
                                    Status : status
                                    });
                                    let payload = {
                                        notification : {
                                        title: "Message",
                                        body: msg,
                                         sound:"default",
                                         }
                                    };
                                    console.log(payload);
                                    return admin.messaging().sendToDevice(token,payload).then((response) =>{
                                    console.log(token);
                                    console.log("Successfully sen notification");
                                    }).catch(function(error){
                                    console.warn("Error sending notification",error);
                                    });


                     }


                    });

在 d1 中的数据是

{ Invite2: { PhoneNumber: 917893659558, Amount: 33 },
  Invite1: { PhoneNumber: 917799266509, Amount: 33 },
  Invite3: { Amount: 33, PhoneNumber: 918639146409 } 
}

【问题讨论】:

  • 这就是你的全部代码吗?如果是这样,那是因为 for 循环实际上是在您将数据分配给 d1 之前执行的。因为 get 操作是异步的。
  • Yaa 这是我写的代码我没有得到 d1 中的数据
  • 是的,因为 get 是异步的。尝试在 for 循环的正上方放置一条日志语句。您会在获取文档之前看到它的日志。
  • 我检查了它,它打印出未定义的如何解决这个问题

标签: firebase firebase-realtime-database firebase-cloud-messaging google-cloud-functions google-cloud-firestore


【解决方案1】:

当您获得带有.get 的文档时,必须从数据库中获取该文档。因此,此操作是异步的,您必须等到收到文档后才能迭代其数据。简而言之,它应该如下所示:

some_doc_ref.get().then(doc => {
  if (doc.exists) {
    var d1 = doc.data();

    for(var k in d1) {
      //...
    }
  }
});

希望对您有所帮助。

【讨论】:

    【解决方案2】:

    使用 Promise.all

    let promise = [];
    let all = [];
    for(var k in d1){
        var p = d1[k].PhoneNumber;
        let rph = respond.where(receiverph ,"==", p)
            .set({ Status : status });
            let payload = {
                notification : {
                title: "Message",
                body: msg,
                    sound:"default",
                    }
            };
            console.log(payload);
            return admin.messaging().sendToDevice(token,payload).then((response) =>{
            console.log(token);
            console.log("Successfully sen notification");
            }).catch(function(error){
            console.warn("Error sending notification",error);
            });
        }
        promise.push(rhp);
    });
    
    Promise.all(promise).then((data)=>{
         data.forEach(query => {
           query.forEach(res=>{
              all.push(res.data());
         })
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 2018-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多