无法使用ref mongoose保存文档

时间:2017-05-19 09:04:38

标签: node.js mongodb express mongoose

我无法保存插入

我的模特: 行动和类型干预

 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);
 /*--------------------------------------*/

我的控制者:

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:错误被捕获(new_intervention.action不是apear,类型是未定义的!?) 我认为这是一个问题

  { "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"
    }

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:

保存之前:我尝试找到action_id

alert(subListToShow.closest('span.additional-background ms-navedit-flyoutArrow').html());