javascript - Mongoose model not including added value when returned -
i trying 2 queryies , combine them , send client... here middleware:
exports.shipmentbyid = function(req, res, next, id) { shipment.findbyid(id) .populate('user', 'displayname') .exec(function(err, shipment) { if (err) return next(err); if (! shipment) return next(new error('failed load shipment ' + id)); vendorinvoice.find({ shipment: id }) .exec(function (err, vendorinvoices) { if (err) return next(err); if (! vendorinvoices) return next(new error('failed load shipment ' + id)); shipment.vendorinvoices = vendorinvoices; req.shipment = shipment; next(); }); }); }; and controller method called
exports.read = function(req, res) { console.log(req.shipment.vendorinvoices); // note prints data looking res.jsonp(req.shipment); }; but on client of values vendorinvoices:
{ __v: 0 _id: "5583af682b46ec9353963dc4" created: "2015-06-19t05:58:00.434z" dateinvoiced: "2015-06-18t07:00:00.000z" shipmentid: "12345" user: {_id: "549268852f54d06f4d0720ce", displayname: "troy cosentino"} } i'm stuck, why wouldn't pass through?
you try returning plain javascript object instead of mongoose model instance calling lean() method on query chain follows:
shipment.findbyid(id) .populate('user', 'displayname') .lean() .exec(function(err, shipment){ if (err) return next(err); if (! shipment) return next(new error('failed load shipment ' + id)); vendorinvoice.find({ shipment: id }) .exec(function (err, vendorinvoices) { if (err) return next(err); if (! vendorinvoices) return next(new error('failed load shipment ' + id)); shipment.vendorinvoices = vendorinvoices; req.shipment = shipment; next(); }); });
Comments
Post a Comment