node.js 即使提供了文件,在猫鼬中也出现验证错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27244849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting a validation error in mongoose even though files are provided
提问by Bazinga777
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'
});
}
});
})
回答by kaxi1993
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'
});
}
});
});
回答by user2662006
You need to install ( npm install body-parser --save), and use it in your express server as below (app.js). hope this answer help someone :)
您需要安装(npm install body-parser --save),并在您的快速服务器中使用它,如下所示(app.js)。希望这个答案对某人有所帮助:)
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
app.use(bodyParser.json())
app.use('/api',api)
module.exports = app

