【发布时间】:2021-06-27 05:10:03
【问题描述】:
我正在尝试通过订单文档给出的 restaurantId 获取餐厅名称。
问题:Restaurant.findOne() 返回一个未解决的 Promise。我无法异步运行 find 方法。
Order.find({ customerId: userId })
.exec()
.then((docs) => {
res.status(200).json({
count: docs.length,
orders: docs.map((doc) => {
let rest = Restaurant.findOne({ doc.restaurantId }).exec();
return {
_id: doc._id,
restaurantName: rest,
itemTotal: doc.items.length,
timestamp: doc.timestamp,
orderStatus: doc.orderStatus,
};
}),
});
});
};
【问题讨论】:
-
您可以尝试先将 Order.find 的输出放入一个数组中。然后,迭代数组,以查询餐厅。或者,您可以使用聚合
$lookup在单个查询中返回结果。