未定义的返回函数nodejs

时间:2018-07-20 16:58:24

标签: javascript node.js

我有问题,我正在学习,答应,回调,但我仍然感到困惑。如何通过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

0 个答案:

没有答案