[英]Javascript function returning undefined from mongodb function
Hi I get data from mongoDB and I try insert new data from other function. 嗨,我从mongoDB获取数据,然后尝试从其他函数插入新数据。 For some reason I get undefined. 由于某种原因,我不确定。 What I'm doing is wrong and how can I fix it? 我在做什么是错误的,我该如何解决?
router.post("/list", (req, res, next) => {
const params = req.body;
var results_data = [];
Countability.find()
.populate("user")
.populate("company")
.populate("store")
.exec(function (err, data) {
_.forEach(data, function (value, key) {
results_data[key] = {
_id: value._id,
count: GetCount(10)
};
});
res.status(200).json({ status: 1, result: results_data });
});
}); });
const GetCount = (id) => {
DataRow.aggregate(
[
{
"$group": {
"_id": "$rowName",
"count": { "$sum": 1 }
}
}
]).exec((err,res) => {
var data = Promise.all([res]);
data.then(function (res) {
return res
})
})
}
Though the forEach
has not finished,server starts sending response. 尽管forEach
尚未完成,但是服务器开始发送响应。 You need to make sure the results_data
has got all data,then call res.status(200).json
你需要确保results_data
已经得到了所有数据,然后调用res.status(200).json
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.