node.js - 嵌套数组上的猫鼬“填充”返回 null
问题描述
我最近开始populate
对我的一个 API 请求使用 mongoose 选项,该选项适用于标准对象和模式,但我正在努力使其适用于嵌套数组。
我的架构如下所示(这是我尝试使用populate检索的架构):
const FoodSchema = new Schema( {
name: {type: String, required: true},
price: {type: String, required: true},
category: {type: String, required: true},
...
})
然后我有:
const OrderFoodSchema = new Schema(
{
food: {type: Schema.Types.ObjectId, required: true, ref: 'food'},
quantity: {...},
price: {...},
},
{ _id: false });
&&
const OrderSchema = new Schema( {
customer: {...},
venue: {type: Schema.Types.ObjectId, required: true, ref: 'venue'},
foods: [OrderFoodSchema]
})
我获取数据的查询是:
return Order.findOne({ _id: order_id, customer: user._id })
.populate({path:'venue',select:['name','address','contact']})
.orFail()
.then(function(order) {
res.json(order)
})
.catch(next);
上面的填充适用于场地(可能是因为我只填充了一层)。但我似乎无法为食物找到一个有效的人口,它总是返回空值。
我努力了:
.populate('foods.food')
.populate({path:'foods.food'})
.populate({
path: 'foods',
populate: {
path: 'food',
}
})
我怎样才能做到这一点?谢谢。
解决方案
推荐阅读
- python - Django:根据用户显示不同的内容
- javascript - 未捕获的 SyntaxError:正则表达式上的意外标记
- node.js - 节点中的不可变对象应该声明为 let、var 还是 const?有什么区别吗?
- .net - 删除捆绑包但保持其已安装的 msi
- html - 我做错了什么?HTML + CSS 按钮
- android - 刷新令牌时Dagger 2获取旧令牌
- eloquent - 如何使用 eloquent 在 laravel 的控制器中使用数据库中的数据
- c# - 是否可以统一序列化精灵?
- java - Kotlin 可比运算符来检查两个对象是否相等
- mysql - 如何修复错误“此 Web 应用程序实例已停止。无法加载 []”