2

To get a data from the firestore I queried a document and get that data into variable. Now, I need to use that variable in other part of the code. when I am using that variable it does not get any data . How to resolve these error.

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);
                                    });

Here in for loop, I am using the d1 variable. But it is not executing these for loop

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);
                                    });


                     }


                    });

In d1 the data is

{ Invite2: { PhoneNumber: 917893659558, Amount: 33 },
  Invite1: { PhoneNumber: 917799266509, Amount: 33 },
  Invite3: { Amount: 33, PhoneNumber: 918639146409 } 
}
Lahari Areti
  • 627
  • 2
  • 10
  • 30

2 Answers2

1

Use Promisse.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());
     })
})
Elialber Lopes
  • 633
  • 7
  • 18
0

When you get a document with .get, the document has to be fetched from the database. Therefore this operation is asynchronous and you must wait until the document is received before you can iterate on its data. In short, it should look something like the following:

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

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

Hope that helps.

Vincent
  • 1,553
  • 1
  • 11
  • 21