Last Updated: February 25, 2016
·
3.178K
· klclee

mongoose: Cast to ObjectId failed for value "[object Object]" at path "_id"

As a newbie mongoose user this was a bit abstract for me. If you get the above error is likely that you sub document have been 'created' yet so either:

if you sub document have a sperate defined schema:

var sub_schema = mongoose.model('Sub_Schema', Sub_Schema);
var Sub = new sub_schema();
Sub.set_data;
parent.children.push(Sub);
parent.save();

Or if your sub document is defined within the parent (version 3.x onwards):

var parent = new Parent();
var sub_doc = parent.children.create({data: 'gggg'});
original_parent.children.push(sub_doc);
original_parent.save();