Cannot call method path on undefined when populating

1,050 views
Skip to first unread message

TylerS

unread,
Feb 1, 2012, 1:53:43 AM2/1/12
to mongoo...@googlegroups.com
I have two schemas, defined like so (extra fields ommitted): 

Transaction = new Schema({
'lineItems' : {type: [LineItem]},
'orderNumber' : { type: Number}
});

LineItem = new Schema({
'product' : {type: Schema.ObjectId,ref: 'Product'}
});

I then have a query, which is attempting to populate the lineItems.product documents:

var qry = app.Transaction.findOne({orderNumber : req.params.orderNumber})
.populate('lineItems.product')
.run(function(err,transaction) {
res.render('store/receipt', { transaction : transaction})
})

This is throwing the following error when it tries to populate lineItems.product:

[ERROR] TypeError
TypeError: Cannot call method 'path' of undefined
    at /home/tyler/projects/prescribed/node_modules/mongoose/lib/model.js:233:44
    at /home/tyler/projects/prescribed/node_modules/mongoose/lib/model.js:239:14
    at Array.forEach (native)

When I debugged into it, I found that the subschema on line 233 is undefined, however I'm not sure how this could be? 

Arlo

unread,
Feb 1, 2012, 6:33:45 AM2/1/12
to mongoo...@googlegroups.com
I've run into this crash tonight as well. I'm using the current version of Mongoose.

My structure seems to be identical (this is trimmed):

The Models:

Share = new Schema({
data: String,
comments: [Comment],
user_id: { type: Schema.ObjectId, ref: 'User' }
});

Comment = new Schema({
data: String,
user_id: { type: Schema.ObjectId, ref: 'User' }
});

Inside Node/Express:

Share.findById( req.params.id ).populate('comments.user_id').run( function(err, share) {
res.send(share.comments);
});

Crash:

TypeError: Cannot call method 'path' of undefined
    at /home/ubuntu/www/node_modules/mongoose/lib/model.js:234:44
    at /home/ubuntu/www/node_modules/mongoose/lib/model.js:240:14
    at Array.forEach (native)

Aaron Heckmann

unread,
Feb 1, 2012, 10:25:11 PM2/1/12
to mongoo...@googlegroups.com
In both examples it appears that the schemas are defined in reverse order.

child schemas should be defined first before passing them into parent schemas, otherwise with javascript variable hoisting you are just passing undefined.

LineItem = new Schema({
'product' : {type: Schema.ObjectId,ref: 'Product'}
});

Transaction = new Schema({
'lineItems' : {type: [LineItem]},
'orderNumber' : { type: Number}
});

// LineItem down here is too late since its already been passed into the parent.




--
http://mongoosejs.com
http://github.com/learnboost/mongoose
You received this message because you are subscribed to the Google
Groups "Mongoose Node.JS ORM" group.
To post to this group, send email to mongoo...@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en



--
Aaron


Tyler Schroeder

unread,
Feb 2, 2012, 1:08:35 AM2/2/12
to mongoo...@googlegroups.com
Doh! Thank you, that did the trick. 

Arlo

unread,
Feb 2, 2012, 4:55:26 AM2/2/12
to mongoo...@googlegroups.com
You'd think I'd have known this from having to do the same thing in Django Models.

Whew... thanks!
Reply all
Reply to author
Forward
0 new messages