我试图在mongoose中将几个评论对象推送到我的UserSchema中。但是,每当我运行此代码时,它只会为每个注释条目创建“_id”字段,即
function saveUser(user) {
user.save(function (err) {
if (err)
console.log(err);
});
}
function createUser(req, res, callback) {
var userData = new User({username: req.params.username});
getUserComment(req, res, function(testComment) {
userData.comments.push({body: testComment});
saveUser(userData);
});
}
我的架构:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var CommentSchema = new Schema({
comments: {
score: Number,
body: String
}
});
var UserSchema = new Schema({
username: {type: String, unique: true},
comments: [CommentSchema]
}, { collection: 'user'});
module.exports = mongoose.model('User', UserSchema);
当然,我有更多属性,但我只是包含一个片段,表明即使是一个小例子也不起作用。
答案 0 :(得分:0)
还有额外的评论"在调用中因为我如何声明CommentSchema。现在非常明显!