我正在尝试实施一个用户回答问题并获得积分的学习平台。我希望在用户个人资料/集合中保存问题和答案以及用户为获得正确答案而尝试的次数。
我的猫鼬模式如下:
username: { type: String, lowercase: true, unique: true },
hash: String,
salt: String,
realname : String,
score: Number,
CourseFinished: Boolean,
questions: [{
question : String,
answer : String,
noOfTries: Number
}]
创建用户的路线就是这个:
router.post('/register', function(req, res, next){
if(!req.body.username || !req.body.password){
return res.status(400).json({message: 'Please fill out all fields'});
}
var user = new User();
user.username = req.body.username;
user.setPassword(req.body.password);
user.questions.question = "What is the language used to write websites?"; //this one doesn't work
user.questions.answer.push("HTML"); //this one doesn't work either
user.save(function (err){
if(err){ return next(err); }
return res.json({token: user.generateJWT()})
});
});
如何通过创建用户来初始化一系列问题和答案?