0

I am developing a web application based on the Mean Stack, but I have a care with Add and Edit operations, particularly with sub documents of the Field "order", the console tells me that "reference" is undefined on the line "order.reference" : req.body.order.reference, , I don't know how to for sub documents. When I add or modify any of "order" fields I got an error, but when I add all the Fields without exception it works. here is my 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 you for your time, I can provide angular code and html

user3665192
  • 315
  • 1
  • 4
  • 8

1 Answers1

0

You'll probably want to use Express and the body parser: How do you extract POST data in Node.js?

Note: I believe this was written for Express 3 and the syntax is a little different for Express 4

Community
  • 1
  • 1