[英]Node/Mongoose Error: ValidationError
This is my server.js. 这是我的server.js。 When I run node server.js then use PostMan to post json, it gives me the following error. 当我运行node server.js然后使用PostMan发布json时,它给了我以下错误。
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
app.use(bodyParser.json())
app.get('/api/posts', function(req, res) {
res.json([
{
username: '@rodandrew95',
body: 'node rocks!'
}
])
})
app.listen(3000, function() {
console.log('Server listening on', 3000)
})
var Post = require('./models/post')
app.post('/api/posts', function(req, res, next) {
// console.log('post received')
// console.log(req.body.username)
// console.log(req.body.body)
// res.sendStatus(201)
var post = new Post({
username: req.body.username,
body: req.body.body
});
post.save(function (err, post) {
if (err) { return next(err) }
res.sendStatus(201).json(post)
})
})
The error: 错误:
(node:6863) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
ValidationError: Post validation failed
at MongooseError.ValidationError (/Users/andrewrodrigues/Desktop/write_modern/ch_1/node_modules/mongoose/lib/error/validation.js:23:11)
at model.Document.invalidate (/Users/andrewrodrigues/Desktop/write_modern/ch_1/node_modules/mongoose/lib/document.js:1486:32)
at /Users/andrewrodrigues/Desktop/write_modern/ch_1/node_modules/mongoose/lib/document.js:1362:17
at validate (/Users/andrewrodrigues/Desktop/write_modern/ch_1/node_modules/mongoose/lib/schematype.js:705:7)
at /Users/andrewrodrigues/Desktop/write_modern/ch_1/node_modules/mongoose/lib/schematype.js:742:9
at Array.forEach (native)
at SchemaString.SchemaType.doValidate (/Users/andrewrodrigues/Desktop/write_modern/ch_1/node_modules/mongoose/lib/schematype.js:710:19)
at /Users/andrewrodrigues/Desktop/write_modern/ch_1/node_modules/mongoose/lib/document.js:1360:9
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
I'm trying to learn the MEAN stack through "Write Modern Web Apps with the MEAN Stack" but I'm running into issues all the time, even when I follow the code and instructions exactly. 我正在尝试通过“用MEAN堆栈编写现代Web应用程序”来学习MEAN堆栈,但是即使我严格按照代码和说明进行操作,我始终都遇到问题。 Can anyone help understand this error, and possibly recommend some good resources for learning the mean stack? 任何人都可以帮助理解此错误,并可能推荐一些好的资源来学习均值堆栈吗?
This error is triggered because you have provided a mongoose validation in your schema (in /models/post
) and that validation is failing. 触发此错误的原因是,您在架构中( /models/post
)提供了猫鼬验证 ,并且验证失败。
For instance, if you provided your model like this : 例如,如果您提供的模型是这样的:
var postSchema = new Schema({
"username": String,
"body": String,
"email": {
type: String,
required: true
}
});
var Post = mongoose.model('Post', postSchema);
This would fail because email
required
validator is not respected. 这将失败,因为不遵守email
required
验证程序。 Find a full list of validators here . 在此处找到验证器的完整列表。
Side note : res.sendStatus(201).json(post)
will set the json body and content-type header after sending a response with 201 status. 旁注: res.sendStatus(201).json(post)
将在发送状态为201的响应后设置json正文和content-type标头。 To send both use : 要同时发送,请使用:
res.status(201).json(post)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.