所以我的应用程序有以下布局:
UserSchema(包含用户名和密码)
var UserSchema = new mongoose.Schema({
firstName: String,
lastName: String,
email: String,
username: String,
password: String
}, options);
UserSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model("User", UserSchema);
带有附加字段的StudentSchema(用户上的discriminator())
var StudentSchema = new mongoose.Schema({
indexNumber: String,
courses: [{
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "Course"
},
entryDate: {
type: Date,
default: Date.now
}
}]
}, options);
module.exports = User.discriminator("Student", StudentSchema);
包含其他字段的TeacherSchema(用户上的discriminator())
var TeacherSchema = new mongoose.Schema({
degree: String,
courses: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Course"
}]
}, options);
module.exports = User.discriminator("Teacher", TeacherSchema);
我正在对User.register()使用passport local-strategy。
router.post("/register", function(req,res) {
var newUser = new User({
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
username: req.body.username
});
User.register(newUser, req.body.password, function(err, user) {
if(err) throw err;
passport.authenticate("local")(req, res, function() {
req.flash("success", "Welcome " + user.username + ".");
res.redirect("/");
});
});
});
现在所有这些都在db中的同一集合用户下(由于鉴别器)
在用户注册后,我如何将“推广”给学生或教师,以便我可以在他的对象中添加其他字段?
答案 0 :(得分:0)
尝试使用用户模型上的__t
命令将update
设置为“教师”或“学生”。我认为默认的鉴别器密钥是__t
所以你需要这样的东西:
user.update({$set: {__t: "Student" } }, callback)`
然后你需要再次使用Discriminator来获取你的字段:
const Student = User.discriminator("Student", StudentSchema);
Student.findById(id, callback)