mongoose populate 似乎不起作用, 另人疑惑的是, 我在项目另一个地方使用,它是正常的问题出现的环境背景及自己尝试过哪些方法mongodb: 4.0.1mongoose: 5.2.9相关代码// dinner.model.jsconst mongoose = require('mongoose')const Schema = mongoose.Schemaconst DinnerSchema = new Schema({ name: { type: String
}, content: { type: [{ type: Schema.Types.ObjectId, ref: 'Food'
}]
}
})const Dinner = mongoose.model('Dinner', DinnerSchema, 'dinners')module.exports = Dinner// food.model.jsconst mongoose = require('mongoose')const Schema = mongoose.Schemaconst FoodSchema = new Schema({
name: { type: String
}
})const Food = mongoose.model('Food', FoodSchema, 'foods')module.exports = Foodrouter.get('/test', async (ctx, next) => { const result = await DinnerModel.find().populate({ path: "foods"
})
ctx.result = { code: 200, data: { list: result, msg: 'success'
}
} await next()
})
1 回答
明月笑刀无情
TA贡献1828条经验 获得超4个赞
const result = await DinnerModel.find().populate({ path: "foods" // => 修改成 path: "content" 试试})
- 1 回答
- 0 关注
- 1190 浏览
添加回答
举报
0/150
提交
取消