node.js - Getting a validation error in mongoose even though files are provided -
i getting validation error following message on saving database though have provided fields,
{ [validationerror: validation failed] message: 'validation failed', name: 'validationerror', errors: { name: { [validatorerror: path `name` required.] message: 'path `name` required.', name: 'validatorerror', path: 'name', type: 'required', value: undefined } } }
this how object trying save looks
{ 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 schema
// load things need var mongoose = require('mongoose'); // define schema 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 model users , expose our app module.exports = mongoose.model('appointment', appointmentschema);
here route file
app.post('/saveappointment', function(req, res) { var appointment = new appointment(); var appointment = { //need add 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' }); } }); })
try code below , let me know if same error appear
app.post('/saveappointment', function(req, res) { var appointment = new appointment({ //need add 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' }); } }); });
Comments
Post a Comment