Cannot call method path on undefined when populating

瀏覽次數:1,050 次
跳到第一則未讀訊息

TylerS

未讀,
2012年2月1日 凌晨1:53:432012/2/1
收件者: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

未讀,
2012年2月1日 清晨6:33:452012/2/1
收件者: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

未讀,
2012年2月1日 晚上10:25:112012/2/1
收件者: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

未讀,
2012年2月2日 凌晨1:08:352012/2/2
收件者:mongoo...@googlegroups.com
Doh! Thank you, that did the trick. 

Arlo

未讀,
2012年2月2日 凌晨4:55:262012/2/2
收件者:mongoo...@googlegroups.com
You'd think I'd have known this from having to do the same thing in Django Models.

Whew... thanks!
回覆所有人
回覆作者
轉寄
0 則新訊息