I am using mongodb find query inside loop as I need to run the find query 5 times. And I used below code for that:
let result = {};
let miles = ['5','10','15','25'];
let i = 0;
while (i < miles.length) {
Shops.find({ 'shopInfo.address':{ $geoWithin:{ $centerSphere: [ [ 75.83183541365247, 30.902146005639267 ], miles[i] / 3959 ] } } }).then(response=>{
if(i==4){
result[miles[i]] = response.length;
res.json(result);
}else{
result[miles[i]] = response.length;
i++;
}
})
.catch(err=>{
console.log(err)
});
}
And when I hit the api on browser. It's not returning with anything and getting below error in console:
Please help me, How can I solve the issue?
