javascript - using populate to display username instead of id -
i'm running expressjs mongoosejs made connection between collections customerid below:
. . /** * customer schema */ var customerschema = new schema({ id : number, name: string, joined: { type: date, default: date.now }, city: string }); mongoose.model('customer', customerschema); . . /** * order schema */ var orderschema = new schema({ id : number, products: [schema.types.mixed], total: number, comments: string, customerid: {type: number, ref: 'customer'} }); mongoose.model('order', orderschema); . . exports.customerorders = function (req, res) { return order.find({customerid: req.params.customerid}, function (err, orders) { order.populate(orders, {path: 'customerid', model: 'order'}, function (err, orders) { if (!err) { return res.json(orders); } else { return res.send(err); } }); }); };
the above code generate following error:
{ message: "cast objectid failed value "1" @ path "_id"", name: "casterror", type: "objectid", value: 1, path: "_id" }
the relation between objects id
no _id
please me use populate method in right way.
thanx,
mongoose's populate
functionality supports using _id
field find related doc in referenced collection.
so can't use field id
, you'd need change customerid
be:
customerid: {type: objectid, ref: 'customer'}
in orderschema
, populate _id
value of customer instead.
Comments
Post a Comment