MongoDB文档未保存但显示成功

时间:2018-01-02 15:22:01

标签: mongodb axios

我有问题。我试图从调查中保存答案。我的问题是我通过axios发送这些答案,并且它们在服务器的console.log中显示,这意味着我的数据安全地到达了服务器。但是没有保存数据。也没有显示错误。 的 Server.js

router.route('/answers')
    //post answers to database
    .put(function(req, res){
        var survey = new Survey();
        survey.name = req.body.name;
        survey.email = req.body.email;
        survey.q_id = req.body.q_id;
        survey.q_text = req.body.q_text;
        survey.answer = req.body.answer;
        console.log(Survey.collection)
        survey.save(function(err){
            if(err){
                console.log(err)
                return ;                
            }
            res.send(survey);
        });
    })

Survey.js(模型)

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = mongoose.Schema.Types.ObjectId;

var question = new Schema({
    q_text:String,
    order: Number,
    options:[{
        type: {type: String},
        value: {type: String}
    }]
});

module.exports = mongoose.model('Question',question);

发布数据的代码

const url = 'http://localhost:3100/api/answers';
axios.put(url, survey)
  .then(res => {
    console.log('Successfully posted');
    console.log(res);
    setSubmitting(false);
    setStatus({submitted: true});
  })
  .catch(err => {
    console.log(err);
  })

1 个答案:

答案 0 :(得分:0)

试试这个:

X-MS-Has-Attach