我无法上传到数据库

时间:2018-09-20 22:51:53

标签: mongodb mongoose schema fetch fetch-api

大家好,我正在尝试将对象数组上传到mongoDB,并上传一个空数组,

这是我的代码:

我的模式:

const ItemSchema = new Schema({
  formName: String,
  inputs: [
    {
      inputLabel: {
        //type: String,
        // required: true
      },

      inputType: {
        // type: String,
        // required: true,
        // enum: ['text', 'color', 'date', 'email', 'tel', 'number']
      },

      inputValue: {
        // type: String,
        // required: true
      }
    }
  ]
});

我的路线:

router.post('/', (req, res) => {
  const newItem = new Item({
    input: req.body,
    inputLabel: req.body.inputLabel,
    inputType: req.body.inputType,
    inputValue: req.body.inputValue
  });
  newItem.save().then(item => res.json(item));
});

该对象正在尝试推入数据库:

 inputs: [
      {
        inputLabel: 'label',
        inputType: 'text',
        inputValue: 'value'
      }
    ]

这是函数:

sendingData = () => {
    var inputs = this.state.inputs;
    console.log(inputs);
    fetch('/api/items', {
      method: 'POST',
      body: JSON.stringify({ inputs: inputs })
    });
  };

它的发布没有问题,但是我在数据库中得到一个空数组,有人知道为什么吗?

0 个答案:

没有答案
相关问题