我有问题,我正在学习,答应,回调,但我仍然感到困惑。如何通过callback
函数设置范围外变量?
ratingController.js
exports.getRating = (restoranIds, callback) => {
Rating.aggregate([{
$match: {
restoranId: restoranIds
}
},
{
$group: {
_id: "$restoranId",
rating: {
$avg: "$nilai"
}
}
}
], (err, isRating) => {
if (err) {
callback(0)
} else {
callback(isRating[0].rating)
}
});
}
app.js
let ratingValue = 0;
ratingController.getRating(resDataNear.id, (err, res) => {
if (err) {
ratingValue = 0;
} else {
ratingValue = res[0].rating;
}
});
错误
console.log(ratingValue); \\ undefined