[英]promise all mongoose find return incomplete value
我尽一切努力使 promise 循环形成两个查询。 我做了一个查询来调用另一个查询。 但是返回的值只会返回第一个查询。 我不能把这两者结合起来。 这是我的代码
exports.beschikbaarheid = function (req, res, next) {
const { leverancier, start, eind } = req.body;
Categorie.find({
leverancier: leverancier,
})
.exec()
.then((result) => {
return Promise.all(
result.map(async (record) => {
const voor = await Voorraad.find({
categorie: record._id,
}).exec();
record.voorraad = voor;
return record;
})
).then((results) => {
res.json({
results,
});
});
})
};
当我返回记录时,它只会返回“Categorie.find”的查询当我只返回“voor”时,它将返回来自“Voorraad”的所有值以及来自“Categorie”的组,但该值不会联系任何数据来自“类别”
在这种情况下,您应该改用$lookup 。 像这样的东西:
Categorie.aggregate([
{$match: {leverancier: leverancier}},
{
$lookup: {
from: "voorraads", // collection of model Voorraad
localField: "_id",
foreignField: "categorie",
as: "voorraad"
}
}
])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.