0
async onclick(){
  let number=this.state.phoneNumber
  if(number !== null && number.length===10){
    console.log('number',number)
    await firestore().collection('profile')
      .where('mobile', '==', number).get().then(snapshot => {
        if (snapshot.empty) {
        }
        else{
          console.log(snapshot.data())
          
          const res=Promise.resolve(async function(){
            let data=[]
            await  snapshot.forEach(doc => {
              data.push({data:doc.data().uid})
            })

            return data
          })
          if(res.length !==0){
            res.then((pra)=>{
              let payload={Requester:auth().currentUser.uid,Responder:pra}
              firestore().collection('requests').add(payload).then(function() {
                goHome();
              });
            })
          }
        }
      })
  }
  else{
    showSnackbar('Please enter valid mobile number');
  }
}

this is the my code here here i want to wait for foreach to complete the after this is want to save the data received from the this foreach to firebase but issue is that before the foreach finish it is running the quert so i am getting null data in firebase

emppeak
  • 158
  • 12
  • 1
    Does this answer your question? [Using async/await with a forEach loop](https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop) – clod9353 Jan 26 '21 at 07:48

2 Answers2

0

The issue is not async/await, your res is a Promise, and the length will always be undefined. For example:

var p = Promise.resolve([1,2,3]);
console.log(p.length) // undefined
Yi Zhou
  • 803
  • 1
  • 8
  • 24
0

Could you try with this correction?

const res = await Promise.all( snapshot.map( doc => doc.data().uid ) );
farvilain
  • 2,552
  • 2
  • 13
  • 24