Hoa
Hoa

Reputation: 20438

In MongoDB, how can I retrieve an item and then add to an embedded document within it?

I'm trying to do something similar to the following

http://mongoosejs.com/docs/embedded-documents.html

However instead of new BlogPost I'm trying to retrieve then add as follows

function addComment(id, comment, callback) {
    Post.findOne(id, function(err, post) {
    post.comments.push(comment);
    });
}

I'm getting

TypeError: Cannot call method 'call' of undefined

What am I doing wrong? I left out the saving code for simplification it is crashing even without trying to save.

Upvotes: 0

Views: 102

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311835

Assuming that the id parameter is an ObjectId or string (and not a query object), you should be calling findById instead of findOne.

Upvotes: 1

Related Questions