I am develop a small app, I have a problem. After I await to run query from Mongodb then I use map method to do some task then I return my response, but the response always return promise and I have not necessary data. Here my code
exports.getListChallenges = async function (req, res) {
var status_code = Consts.STATUS_CODE.ERROR.UNKOWN_ERROR;
try {
var page_number = parseInt(req.params.page_number);
var page_size = parseInt(req.params.page_size);
var challenges = await Challenge.find()
.skip(page_size * (page_number - 1))
.limit(page_size);
status_code = Consts.STATUS_CODE.SUCCESS.DATA_FOUND;
challenges = challenges.map(async function (o) {
var challenger_club = await Club.findById(o.challenger_club_id);
var challege = {
challenge: o,
challenger_club_name: challenger_club.club_name,
challenger_club_avatar: challenger_club.avatar
};
return challege;
});
res.json({
"status": true,
"message": "Challenges found !",
"status_code": status_code,
"challenges": challenges
});
} catch (error) {
status_code = Consts.STATUS_CODE.ERROR.UNKOWN_ERROR;
res.json({
"status": false,
"status_code": status_code,
"error": error
})
}
}
the "challenges" in response always empty , how can I solve it ? thank !
challenges = await Promise.all( challenges.map( async ...