我一直在阅读关于这个问题的一些答案,但我仍然无法让它工作。填充不检索整个引用的对象只是ID
我的模型对象没有深度嵌套,而且非常简单。这是具有参与其中的用户列表的事件以及具有他们参加的活动列表的用户列表。像这样:
let DinnerSchema = new mongoose.Schema({
date: {
type: Date,
unique: true,
timestamps: true,
required: true
},
title:{type: String, require: true},
attending: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}]
})
和用户:
let UserSchema = new mongoose.Schema({
email: {
type: String,
lowercase: true,
unique: true,
required: true
},
name:{ type: String, require: true },
password: {type: String ,required: true},
dinners: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Dinner'
}]
})
并且为了清楚这里是一个的使用填充整个路线:
userpage.get('/', authCheck, (req, res) => {
const options = { _id: '57ebbf48bd6914036f99acc7' }
return Dinner
.findOne(options)
.populate('User', 'name') //I'VE TRIED ADDING 'name' BASED ON SOME ANSWERS I SAW
.exec((err, newDinner) => {
if (err) {
console.log(err)
res.status(400).end()
}
console.log(newDinner) // SHOW'S USERS ID'S BUT NO OTHER FIELDS
return res.json({
sucsess: true,
data: newDinner
})
})
})
如果我在数据库本身正确理解应该只有是对其他模型的引用,而不是实际上所有的字段,并且连接与填充发生。我的db结构显示只是参考,所以没关系。
我试过指定我后面的字段的名称(在这种情况下的名称字段),但没有奏效。
我的群结果总是看起来像下面并没有显示除了_id一个任何其他领域:
{
_id: 57ebbf48bd6914036f99acc7,
date: 2016-09-27T22:00:00.000Z,
title: '1',
__v: 0,
attending: [ 57ebbcf02c39997f9cf26891, 57ebbdee098d3c0163db9905 ]
}
什么我搞砸了这里?