1
我正在嘗試創建一個電子學習應用程序,這是我的模式,但是現在的問題是,現在只要我嘗試在MongoDB中呈現用戶的課程,並且它顯示的課程是特定用戶正在擁有。填充不適用於Mongoose總是返回undefined
的數據是數組
"courses" : [ "Learn how to be awesome", "Learn how to be a chef" ]
兩個模式
var UserSchema = Schema({
email: String,
password: String,
courses: [{ type: Schema.Types.String, ref: 'Course'}],
});
var CourseSchema = Schema({
title: String,
owners: [{ type: Schema.Types.ObjectId, ref: 'User'}],
body: String,
teacher: { type: Schema.Types.String, ref: 'User'},
});
routes.js的 - 每當我試圖填充用戶的課程對象時,它總是返回undefined
router.get('/my-courses', isLoggedIn, function(req, res) {
User
.findById(req.user.id)
.populate('courses')
.exec(function(err, courses) {
console.log(courses); // always return undefined
res.render('course/student-courses', {
userCourses: courses
});
});
});
即使我已將數據添加到用戶的課程字段中,它總是返回未定義的。
填充只適用於被引用模型的'_id'值,但它看起來像使用'title'值。 –