I am trying to update an existing document by increasing the counter and pushing an object to an array.
Here is the diagram for the user:
var UserSchema = new Schema({ [...] posts: { totalWords: { type: Number, min: 0, default: 0 }, _entries: [ { words: { type: Number, min: 0 }, body: { type: String }, date: Date } ] }, });
And here is the update code:
var newPost = { words: req.body.words, body: req.body.entry, date: new Date() }; User.findOne(req.user._id, function (err, user) { var previous = user.posts.totalWords; user.posts.totalWords = previous + newPost.words; user.posts._entries.push(newPost); user.save(function (err) { if (err) return res.send(400, err); return res.json(newPost); }); });
I get the following error:
[TypeError: Object.keys called on non-object]
Any ideas on how to solve this?
source share