问题描述
您好,我正在学习 node 的基础知识。我遇到的问题是我想验证从 html 表单传递到 post 请求的数据。数据正在传入,但我也在控制台中收到错误。
这是服务器代码:
app.post('/',(req,res)=> {
const schema = Joi.object({
username: Joi.string().trim().required(),password: Joi.string().min(3).max(10).required(),})
const validation = schema.validate(req.body);
console.log(req.body)
if(validation.error){
console.log(validation.error)
// res.send('an error has occurred')
}
res.send('successfully posted data')
})
控制台错误(示例数据:用户名:test & 密码:test):
[Error [ValidationError]: "value" must be of type object] {
_original: [
{ name: 'username',value: 'test' },{ name: 'password',value: 'test' }
],details: [
{
message: '"value" must be of type object',path: [],type: 'object.base',context: [Object]
}
]
}
我不明白为什么我会收到验证错误。当使用 console.log(req.body) 打印到控制台时,req.body 似乎是一个对象,但是当我尝试使用验证时,它出现在数组中?有点糊涂。
附加信息: 如果重要的话,我正在使用 express.urlencoded() 和 express.json()。
这是来自 HTML 页面的 JQuery:
<script>
$(document).ready(()=>{
$('#form').submit((e)=>{
e.preventDefault();
$.ajax({
url: '/',type: 'post',contentType: 'application/json',data: JSON.stringify($('#form').serializeArray()),success: (response)=>{
console.log('successfully got response');
console.log(response)
}
})
})
});
</script>
解决方法
错误明确指出 req.body
是一个数组。
[
{ name: 'username',value: 'test' },{ name: 'password',value: 'test' }
]
从 jQuery documentation page 开始,我们也有 .serializeArray() method creates a JavaScript array of objects
。因此,您实际上是在向服务器端发送一组对象,这就是您遇到错误的原因。
为了解决这个问题,我认为你应该修改前端部分。既然你已经有了
express.urlencoded()
,您可以使用 serialize。
<script>
$(document).ready(()=>{
$('#form').submit((e)=>{
e.preventDefault();
$.ajax({
url: '/',type: 'post',contentType: 'application/json',data: JSON.stringify($('#form').serialize()),success: (response)=>{
console.log('successfully got response');
console.log(response)
}
})
})
});
</script>