【发布时间】:2021-10-20 03:57:18
【问题描述】:
所以基本上我有一个集合,其中包含引用另一个模式中的对象的 id。
如果我有一个包含这些 id 的数组,我如何映射数组并获取与该 id 关联的对象?
这是我目前所拥有的,但响应只是空对象:
// this gives me an array of all the itemIds that a buyer has bought.
const prevPurchases = await Sales.find({ buyerId }).select(["itemId"]);
const item = prevPurchases.map(async (e) => {
try {
const item = await Item.findById(e.itemId).select(["image", "name"]);
return item;
} catch (e) {
return null;
}
});
await Promise.all(item);
return res.status(200).json({ item }); // just returns "{}, {}, {}, etc"
我该如何解决这个问题,以便对象包含我在select 中指定的图像和项目名称。谢谢!
【问题讨论】:
标签: javascript typescript mongodb express