Friday, 19 May 2017

not able to save document with ref mongoose

I can't save my insert

MY MODELS : Action and Type_intervention

 var mongoose = require("mongoose"),
 Schema = mongoose.Schema;

 var actionSchema = new Schema({
    action: {
       type: String,
       required: true,
       unique: true
    },
 }); //Exporter le model


 module.exports = mongoose.model('Action', actionSchema);

 /*-----------------------------------------*/

var mongoose = require("mongoose"),
Schema = mongoose.Schema;

var type_interventionSchema = new Schema({
   type_name_intervention : {type : String},
   libelle : {type : String},
   Standart : {type : String},
   libelle_crt : {type : String},
   action : {type: Schema.ObjectId, ref: 'action'},
 });


//Exporter le model
module.exports = mongoose.model('Type_intervention',type_interventionSchema);
 /*--------------------------------------*/

MY CONTROLLER:

var new_type_intervention = new Type_intervention({
     type_name_intervention: req.body.type_name_intervention,
     libelle: req.body.libelle,
     Standart: req.body.Standart,
     libelle_crt: req.body.libelle_crt,
     action: req.body.action,
  })
new_type_intervention.save((err, newinter) => {
    if (err) {
        return res.send({
            message: 'Error when try to save',
            'intervention': new_type_intervention,
            'req.action': req.body.action,
            'new_type_intervention_action': new_type_intervention.action
        });
    }
    return res.send({
        message: 'Add with succes',
        'intervention': new_type_intervention
    })
})

POSTMAN RUN : The error is catched (new_intervention.action not apear and there type is undefined !? ) I think this is a probleme

  { "type_name_intervention":"f",
    "libelle":"f",
    "Standart":"f",
    "libelle_crt":"f",
    "action":"test"}

   //Results:
   {
     "message": "Error when try to save",
      "intervention": {
      "type_name_intervention": "f",
      "libelle": "f",
      "Standart": "f",
      "libelle_crt": "f",
      "_id": "591eb2ccd4325d0e40b2d038"
    },

    "req.body.action": "test",
    "type of new_type_intervention_action": "undefined"
    }



via Anis Mokeddes

No comments:

Post a Comment