0
我是Mongo和Express的新手。我正在构建一个简单的应用程序,让公司向员工发送问题,请求反馈。我努力将员工(用户)作为一个数组放在问题文档中。这是我的模式[不知道我是否正确写入]。POST数据不会保存阵列
//question schema
var QuestionSchema = Schema({
id : ObjectId,
title : String,
employees : [{ type: ObjectId, ref: 'User'}]
});
module.exports = mongoose.model('Question', QuestionSchema);
//user schema
var UserSchema = Schema({
username : String,
response : String,
questions : [{ type: ObjectId, ref: 'Question'}]
});
module.exports = mongoose.model('User', UserSchema);
api.js
router.post('/', function (req, res) {
// make sure user is authenticated
User.findOne({ username: req.body.username }).exec(function(err, user) {
if(err) throw err;
if(!user) {
res.json({ success: false, message: 'Could not authenticate user' })
} else if (user){
/*----------------save example----------------------*/
var question = new Question({ title: 'Should we buy a coffee machine?'});
question.save(function (err) {
if (err) throw err;
var user1 = new User({
username: "marcopolo",
response: "yes",
});
user1.save(function (err) {
if (err) throw err;
});
});
console.log('entry saved >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
}
});
});
UPDATE(雇员表)
您确定QuestionSchema中的自动实现的ID正在工作吗?我指的是'ObjectId' – Jesper
@Jesper是的。我添加了第二个快照来显示其中的员工表具有相同的问题。不显示在问题阵列 –