I am trying to push several comment objects into my UserSchema in mongoose. However, whenever I run this code, it only creates the "_id" fields for each comment entry, ie
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);
});
}
My schemas:
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);
Of course, I have more properties but I just included a snippet to show that even one small example does not work.
There was an extra "comments" in the call because of how I declared the CommentSchema. Painfully obvious now!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.