所以我试图通过mongoose和express来创建mongodb中的记录,但是我收到了验证错误:
ValidationError: User validation failed: name: Path `name` is required.,
email: Path `email` is required., password: Path `password` is required.
at ValidationError.inspect (D:\Projects\addlife\node_modules\mongoose\lib\error\validation.js:56:24)
at formatValue (util.js:430:38)
at inspect (util.js:324:10)
at format (util.js:191:12)
at Console.log (console.js:130:21)
at D:\Projects\addlife\app.js:104:21
这是mongoose架构:
let mongoose = require('mongoose');
//User Schema
let userSchema = mongoose.Schema({
name:{
type: String,
required: true
},
email:{
type: String,
required: true
},
password:{
type: String,
required: true
}
});
let User = module.exports = mongoose.model('User', userSchema);
这是提交邮寄路线:
app.post('/', function(req, res){
let user = new User();
user.name = req.body.name;
user.email = req.body.email;
user.password = req.body.password;
user.save(function(err){
if(err){
console.log(err);
return;
}else{
res.redirect('/');
console.log('redirected');
}
});
});
似乎无法调试mongoose验证错误。
答案 0 :(得分:0)
您是否在请求中提供了名称和密码参数?这可能是由于您未在帖子请求中提供这些变量的结果。