My async function either can't access the updated version of the found var, or it's resolving before the async calls are complete. I can't tell what the problem here is, and why it's not working
If tried calling the return true at various different places, and even time delaying it, but inside the handler function it's always empty.
I can't understand why it's not able to access the updated var.
No amount of formatting, or syntax changes seem to fix it.
router.get('/search', (req, res) => {
var country = req.query.country;
var found = [];
var countrySearch = () => {
if(country==="Worldwide"){return true}
conf.find({country: { "$regex": country, "$options": "i" }}, function(err, conferences){
if(found.length!=0){
for(conference in conferences){
for(item in found){
if(found[item]._id!=conferences[conference]._id)
found.push(conferences[conference])
}
}
return true;
}else{
for(i=0;i<=conferences.length;i++)
found.push(conferences[i])
return true;
}
})
}
function resolve(){
res.render('index', {list: found});
}
async function handler(){
const place = await countrySearch();
return resolve();
}
handler();
})
If console.log the output of found after pushing to it, inside the Async function, it returns correctly with the appended items.
When passing it to Pug as a variable, and anywhere inside the resolve function, the found var appears empty.
Solved.
Function != Promise;
var countrySearch = conf.find({country: { "$regex": country, "$options": "i" }}, function(err, conferences){
if(country==="Worldwide"){return true}
if(found.length!=0){
for(conference in conferences){
for(item in found){
if(found[item]._id!=conferences[conference]._id) found.push(conferences[conference])
}
}
return true;
}else{
for(i=0;i<=conferences.length;i++){
found.push(conferences[i])
if(i===conferences.length) return true;
}
}
})