不能将计数查询的值推入内部以在Mongoose上循环结果。为什么?
当我尝试在count控制台中运行时,该查询正在运行。但除此之外,无法获得计数值。
Stream.find(searchquery).populate('user_id').sort(mysort).skip(offset).limit(limit).exec(function(err, result) {
let streamsarr = [];
async.forEachOf(result, (value, key, callback) => {
//console.log(key);
streamsarr[key] = {};
streamsarr[key]["streamid"] = value._id;
streamsarr[key]["stream_name"] = value.stream_name;
streamsarr[key]["userid"] = value.user_id._id;
streamsarr[key]["status"] = value.status;
streamsarr[key]["watching"] = 0;
User.count({watching: value._id}, function(err, count) {
console.log(count);
streamsarr[key]["watching"] = count;
})
}, function allDone (err) {
});
res.json(streamsarr);
});