I want the code to wait for the foreach function to complete before going to the next line to perform the if statement on the counter number. I am sure I have to put a Promise somewhere... but I also thought that .then() would do the trick?
It appears there are examples of this but I am not able to figure this one out or where Im going wrong here:
async getSomeData () {
const data = [];
const ref = fire.firestore();
ref
.collection(‘foo’)
.where(‘open’, '==', true)
.get()
.then(async snapshot => {
let count = 0;
snapshot
.docs
.forEach(async doc => {
const {foo, bar} = doc.data();
const number_count = await this.getNumber(doc.id);
if (number_count >= 1){
count++;
data.push({
foo,
bar
});
this.setState({
data : data,
});
}
})
.then() ?????
**//THIS IS EXECUTING BEFORE THE FOREACH FINISHES**
if(count==0){
this.setState({
isLoading: false,
noStores: true
});
}
}).catch(error => {
console.log(error);
//if fails
this.setState({
noStores : true,
});
});
};
Appreciate any help I can get. Thanks!