how to resolve error in sub documents mongoose/express -
i developing web application based on mean stack, have care add , edit operations, particularly sub documents of field "order", console tells me "reference" undefined on line "order.reference" : req.body.order.reference, , don't know how sub documents. when add or modify of "order" fields got error, when add fields without exception works. here mongoose diagram:
var mongoose = require('mongoose') , schema = mongoose.schema; var contactschema = new schema({ name: {type: string}, order: { reference : {type : string}, adresse : {type : string} , product : {type : string} } }); var contactmodel = mongoose.model('contact', contactschema); mongoose.connect('mongodb://localhost/contact'); exports.add = function(req, res) { var contact = req.body; contact = new contactmodel({ name: req.body.name, "order.reference" : req.body.order.reference, "order.adresse" : req.body.order.adresse, "order.product" : req.body.order.product }); contact.save(function (err) { if (!err) { res.json(true); } else { console.log(err); res.json(false); } }); return res.jsonp(req.body); }; exports.edit = function (req, res) { var id = req.params.id; if (id) { contactmodel.findbyid(id, { upsert: true }, function (err, contact) { contact.name = req.body.name, contact.order.reference = req.body.order.reference, contact.order.adresse = req.body.order.adresse , contact.order.product = req.body.order.product contact.save(function (err) { if (!err) { res.json(true); } else { res.json(false); console.log(err); } }); }); } };
thank time, can provide angular code , html
you'll want use express , body parser: how extract post data in node.js?
note: believe written express 3 , syntax little different express 4
Comments
Post a Comment