即使提供了文件,也会在mongoose中获得验证错误

Baz*_*777 5 mongodb node.js express

即使我提供了所有字段,我在保存到数据库时收到以下消息的验证错误,

{ [ValidationError: Validation failed]
  message: 'Validation failed',
  name: 'ValidationError',
  errors: 
   { Name: 
      { [ValidatorError: Path `Name` is required.]
        message: 'Path `Name` is required.',
        name: 'ValidatorError',
        path: 'Name',
        type: 'required',
        value: undefined } } }
Run Code Online (Sandbox Code Playgroud)

这就是我想要保存的对象的样子

    { Name: 'Nobody Singh',
  phone: '+919177121364',
  address: 'flat no 306 koratala apartments\nstreet no 3 ,Himayatnagar, Near Siraj plaza',
  start: '2014-12-03T13:00:00.000Z',
  end: '2014-12-03T15:00:00.000Z',
  details: 'flat no 306 koratala apartments\nstreet no 3 ,Himayatnagar, Near Siraj plaza' }
Run Code Online (Sandbox Code Playgroud)

这是架构

    // load the things we need
var mongoose = require('mongoose');

// define the schema for our user model
var appointmentSchema = mongoose.Schema({
       email: { type: String, default: 'maisnamraj@gmail.com' },          
       name: { type: String, required:true },
       phone: { type:Number },
       address:{ type: String },
       details:{ type: String },
       title:{ type: String, default: "Slot Taken"},
       start: { type:Date},
       end: { type:Date},
       requestedDate: { type:Date, default: Date.now }

});



// create the model for users and expose it to our app
module.exports = mongoose.model('Appointment', appointmentSchema);
Run Code Online (Sandbox Code Playgroud)

这是路径文件

app.post('/saveappointment', function(req, res) {

var appointment = new Appointment();

    var appointMent = {
                      //need to add an email here 
                      name: req.body.name,
                      phone: req.body.phone,
                      address: req.body.address,
                      start: req.body.appointment.start,
                      end:req.body.appointment.end,
                      details:req.body.details,
                      address:req.body.address
                    };

    console.log(appointMent);

    appointment.save(appointMent,
     function(err,resp) {
        if(err) {
            console.log(err);
            res.send({
                message :'something went wrong'
            });
        } else {
            res.send({
                message:'the appointment has bees saved'
            });
        }           

    });
})
Run Code Online (Sandbox Code Playgroud)

kax*_*993 10

请尝试下面的代码,如果出现相同的错误,请告诉我

app.post('/saveappointment', function(req, res) {

  var appointment = new Appointment({
    //need to add an email here 
    name: req.body.name,
    phone: req.body.phone,
    address: req.body.address,
    start: req.body.appointment.start,
    end: req.body.appointment.end,
    details: req.body.details,
    address: req.body.address
  });


  appointment.save(function(err, resp) {
    if (err) {
      console.log(err);
      res.send({
        message: 'something went wrong'
      });
    } else {
      res.send({
        message: 'the appointment has been saved'
      });
    }

  });
});
Run Code Online (Sandbox Code Playgroud)

  • 你的错误是你将对象插入mongoose保存,正确的方法是在mongoose.model对象上调用save函数并提供回调,因为它的参数不是附加对象,请参阅文档:http://mongoosejs.com/docs/models.html (2认同)