我在Node.js中面临一个问题,我正在尝试填充架构。我有两个架构,一个是" UserSchema"的引用,我在这里添加我的代码请任何人帮我解决这个问题。此代码仅在我删除" .populate(' user.email')"部分
Student.find({'Name': req.studentInfo.Name})
.populate('user.email').exec(function(err, numberOfPendingRequests){
if(err){
return next(err);
}else{
console.log('\n\n\nFound unapproved requests\n\n\n');
console.log(numberOfPendingRequests);
req.studentInfo.unapprovedRequests = numberOfPendingRequests;
return next();
}
})
// user.js
var userSchema = mongoose.Schema({
profile_id : String,
token : String,
email : String,
name : String,
provider : String,
detailSubmited: {
type: Boolean,
default: false
}
});
module.exports = mongoose.model('User', userSchema);
// student.js
var studentSchema = mongoose.Schema({
collegeName : String,
courseName : String,
courseYear : String,
user : {
type: mongoose.Schema.Types.ObjectId,
ref : User
}
});
module.exports = mongoose.model('Student', studentSchema);
答案 0 :(得分:0)
如果您希望填充的文档仅返回特定字段,请将字段名称作为填充函数的第二个参数传递。像这样:
Student.find({'Name': req.studentInfo.Name}).populate('user', 'email').exec(function(err, numberOfPendingRequests) {...}
我希望这会有所帮助。