Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a validation error in mongoose even though files are provided

I am getting a validation error with the following message on saving to database even though I have provided all the fields,

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

This is how the object that I am trying to save looks like

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

an here are the schema

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

// define the schema for our user model
var appointmentSchema = mongoose.Schema({
       email: { type: String, default: '[email protected]' },          
       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);

Here is the route file

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'
            });
        }           

    });
})
like image 256
Bazinga777 Avatar asked Dec 02 '14 08:12

Bazinga777


People also ask

How does Mongoose validation work?

If your validator function returns a promise (like an async function), mongoose will wait for that promise to settle. If the returned promise rejects, or fulfills with the value false , Mongoose will consider that a validation error.

What does Mongoose unique validator do?

mongoose-unique-validator is a plugin which adds pre-save validation for unique fields within a Mongoose schema. This makes error handling much easier, since you will get a Mongoose validation error when you attempt to violate a unique constraint, rather than an E11000 error from MongoDB.

What is the need of Mongoose .how it is useful for validation?

Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node. js. It manages relationships between data, provides schema validation, and is used to translate between objects in code and the representation of those objects in MongoDB. MongoDB is a schema-less NoSQL document database.

Does Mongoose throw error?

These are mongoose validation. When we run code to save or update the data, all validator runs and if any issue is found by the validator, mongoose throws the error. But these errors are completely different, different validators send errors in different ways.


1 Answers

Try this code below and let me know if the same error will appear

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'
      });
    }

  });
});
like image 156
kaxi1993 Avatar answered Sep 28 '22 06:09

kaxi1993