Mongoose create方法忽略变量,但不忽略硬编码数据

时间:2017-08-03 00:49:02

标签: javascript node.js mongoose

有人能引导我朝着正确的方向前进吗?几天来我一直被困在这里。我正在使用Express和Mongoose。这是我的控制器,

module.exports.addPoll = function(req,res) {
  var optionsArray = [];
  var row = [];
  req.body.option1.forEach( function(value) {
    optionsArray.push('{"v":"' + value + '"},{"v":0}');
  });
  var beg = '{"c":[';
  var end = ']}';
  var whole = beg.concat(optionsArray, end);

  Pol.create({
  cols: [{label: "options", type: "string"}, {label: "quantity", type: 
   "number"}],
  rows: whole,
  title: req.body.heading
  }, function(err, poll) {
   if (err) {
    sendJSONresponse(res, 400, err);
   } else {
    sendJSONresponse(res, 201, poll);
    console.log(whole);
  }
});
}

当我创建文档时,rows属性为空。它忽略了包含数据的变量,看起来像这样。

{"__v":0,"rows":{"c":[]},"title":"What is tomorrow","_id":"59826915c7a0186940e8431c","cols":[{"label":"options","type":"string"},{"label":"quantity","type":"number"}]}

这是它记录时的样子,如果用这个替换变量,它的效果非常好。我不明白。

[{"c":[{"v":"Monday"},{"v":"0"},{"v":"Tuesday"},{"v":"0"}]}]

这是我的架构。

var rowsSchema = new mongoose.Schema({
c: [{
        v: {
            type: String,
            required: true
        },
        _id:false
    },
    {
        v:{
            type:Number,
            required: true
        },
        _id:false
    }],
_id: false
})

var pollSchema = new mongoose.Schema({
cols: [{
        label: {
            type: String, 
            required: true
        }, 
        type: {
            type: String,
            required: true
        },
        _id: false
    },
    {
        label: {
            type: String, 
            required: true
        }, 
        type: {
            type: String,
            required: true
        },
        _id:false
    }],

rows: rowsSchema,

title: {type: String, required:true, _id:false}
})

0 个答案:

没有答案