I am trying to push into array from loop in expressjs. but it seems not working. everything is fine but "popularDestsResult" gives me blank array.
here is the code.
const getPopularDestsbyCountry = catchAsync(async (req, res) => {
const Country = req.query.country;
const result = await destService.getCountrybyName(Country);
const popularIntl = result.settings.preferred_intl_destinations;
const domIntl = result.settings.preferred_domestic_destinations;
const popularDestsResult = [];
if (popularIntl.length) {
popularIntl.forEach(async function (item) {
const destName = item.destinationName;
const destData = await destService.getSubDestByName(destName);
arrDests = {};
arrDests["destName"] = destData.subdestname;
arrDests["destId"] = destData._id;
arrDests["destCountry"] = destData.parentDest.ParentDestName;
arrDests["destImage"] = destData.banner.imgurl;
arrDests["destType"] = "International"
popularDestsResult.push(arrDests);
//console.log(arrDests)
})
}
if (domIntl.length) {
domIntl.forEach(async function (item) {
arrDests = {};
const destName = item.destinationName;
const destData = await destService.getSubDestByName(destName);
arrDests["destName"] = destData.subdestname;
arrDests["destId"] = destData._id;
arrDests["destCountry"] = destData.parentDest.ParentDestName;
arrDests["destImage"] = destData.banner.imgurl;
arrDests["destType"] = "Domestic"
popularDestsResult.push(arrDests);
//console.log(destData)
})
}
console.log(popularDestsResult);
res.send(popularDestsResult);
});
it gives me blank array. but when i check arrDests in console.log from inside loop, this has data like this. i am not able to understand why push is not working here.
{
destName: 'Dubai',
destId: 6174756806452223d62742cf,
destCountry: 'UAE',
destImage: 'https://thetripclub.fra1.digitaloceanspaces.com/ttcdisk-1635292752476.jpeg',
destType: 'International'
}
{
destName: 'Moscow',
destId: 6174756806452223d62742e6,
destCountry: 'Russia',
destImage: 'https://thetripclub.fra1.digitaloceanspaces.com/ttcdisk-1635360461574.jpeg',
destType: 'International'
}
{
destName: 'Bali',
destId: 6174756806452223d62742da,
destCountry: 'Indonesia',
destImage: 'https://thetripclub.fra1.digitaloceanspaces.com/ttcdisk-1635326282475.jpeg',
destType: 'International'
}
{
destName: 'Paris',
destId: 6174756906452223d6274309,
destCountry: 'France',
destImage: 'https://thetripclub.fra1.digitaloceanspaces.com/ttcdisk-1635361998914.jpeg',
destType: 'International'
}
{
destName: 'Krabi',
destId: 6174756806452223d62742d7,
destCountry: 'Thailand',
destImage: 'https://thetripclub.fra1.digitaloceanspaces.com/ttcdisk-1635346984380.jpeg',
destType: 'International'
}