node.js - ValidationError:后验证失败:标题:需要路径“标题”。在 nodejs api中
问题描述
我正在使用 nodejs 制作一个宁静的 API。在这里,我在实现 multer 之前添加了用于文件上传的 multer,它工作正常,但现在每当我尝试使用创建帖子时,postman
我都会收到类似这样的错误。
错误
{ ValidationError: Post validation failed: title: Path `title` is required.
at ValidationError.inspect (C:\projects\adi-site\api\node_modules\mongoose\l
ib\error\validation.js:59:24)
at formatValue (internal/util/inspect.js:490:31)
at inspect (internal/util/inspect.js:191:10)
at Object.formatWithOptions (util.js:84:12)
at Console.(anonymous function) (console.js:188:15)
at Console.log (console.js:199:31)
at post.save.then.catch.err (C:\projects\adi-site\api\src\routes\posts.js:70
:17)
at process._tickCallback (internal/process/next_tick.js:68:7)
errors:
{ title:
{ ValidatorError: Path `title` is required.
at new ValidatorError (C:\projects\adi-site\api\node_modules\mongoose\lib\error\validator.js:29:11)
at validate (C:\projects\adi-site\api\node_modules\mongoose\lib\schematype.js:975:13)
at C:\projects\adi-site\api\node_modules\mongoose\lib\schematype.js:1028:11
at Array.forEach (<anonymous>)
at SchemaString.SchemaType.doValidate (C:\projects\adi-site\api\node_modules\mongoose\lib\schematype.js:984:19)
at C:\projects\adi-site\api\node_modules\mongoose\lib\document.js:2098:9
at process._tickCallback (internal/process/next_tick.js:61:11)
当我尝试
log
req.body 我得到这个
[Object: null prototype] {
'title ': 'this is title with imahih', <-----//i think here `key` title is in the wrong format I have no idea where it is coming from and how to fix this.
overview: 'this is overview of the image',
content: 'this is image content' }
routes/posts.js <--这是我的代码
router.post('/', upload.single('postImage'),(req, res, next) => {
console.log(req.body);
const post = new Post({
_id: new mongoose.Types.ObjectId(),
title: req.body.title,
overview: req.body.overview,
content: req.body.content,
postImage: req.file.path
});
post.save().then(result => {
console.log(result);
res.status(201).json({
message: "Post created",
createdPost: {
_id: result._id,
title: result.title,
overview: result.overview,
content: result.content,
postImage: result.postImage
}
})
}).catch(err => {
console.log(err);
res.status(500).json({
error: err
})
})
})
这就是我发送请求的方式
解决方案
验证后失败:标题:路径
title
是必需的。
您收到此错误是因为title
邮递员表格后有一个空格。
[Object: null prototype] {
'title ': 'this is title with imahih', // You can notice title being wrapped in '' with extra space
overview: 'this is overview of the image',
content: 'this is image content' }
您可以在邮递员中修剪多余的空间,也可以为此在服务器端处理案例。
'use strict';
router.post('/', upload.single('postImage'), (req, res, next) => {
console.log(req.body);
const formData = Object.keys(req.body).reduce((cleanFormData, attribute) => {
const key = attribute.trim();
cleanFormData [key] = req.body[attribute];
return cleanFormData;
}, {});
console.log(formData);
console.log(formData.title); // logs 'this is title with imahih'
.....
});
推荐阅读
- javascript - 从网页的左侧和右侧删除空格(包括标题)
- powerbi - Power BI 中的动态列值
- aframe - 功能上的 A 帧动画对象
- flutter - 如何在颤动的不同包中的另一个函数中使用来自未来函数的变量
- firefox - Firefox 扩展的 Websocket 连接失败
- organization - 如何从组织中删除用户?
- mocha.js - 如何编写测试用例以检查电子邮件字符串在 JavaScript 中的 Mocha / Chai 中是否有效?
- java - 如何使用 Apache Jmeter 性能测试模拟生产 Spring Boot REST API 的流量(TPS 9.05)?
- ibm-cloud - IBM Watson Clinical Annotator API Python SDK - 添加可选参数会引发 400
- mysql - 搜索特定范围的表 mySQL