提交错误与Joi的前端

发布于 2025-01-28 01:38:21 字数 506 浏览 2 评论 0原文

我有一个注册表格,我正在用Joi验证此表格。 到目前为止,没有问题,我可以反映出JSON的错误,但是我不知道如何在前端打印它,我该怎么做?

我之前使用过Express-validator,那里有一个验证质量结构,因此我可以捕获错误并将其发送到前端。我该如何在这里做到这一点? 你能帮助我吗。

简而言之

const createUserValidation = Joi.object({
    name: Joi.string().required().min(5),
    username : Joi.string().required().min(3),
    password : Joi.string().required().min(8).pattern(new RegExp('^[a-zA-Z0-9]{3,30}$')),
    email: Joi.string().required().email(),

});

,我想将错误从这里返回到前端。

从现在开始谢谢你。

I have a registration form and I am validating this form with joi.
There is no problem so far, I can reflect the errors as json, but I don't know how to print it in the frontend, how can I do that?

I used express-validator before, there was a validationresult structure there, so I could catch errors and send them to the front end. How can I do this here?
can you help me.

Validate process like this

const createUserValidation = Joi.object({
    name: Joi.string().required().min(5),
    username : Joi.string().required().min(3),
    password : Joi.string().required().min(8).pattern(new RegExp('^[a-zA-Z0-9]{3,30}

In short, I want to transfer the errors returned from here to the front end.

Thank you from now.

)), email: Joi.string().required().email(), });

In short, I want to transfer the errors returned from here to the front end.

Thank you from now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风月客 2025-02-04 01:38:21

现在,您必须创建一个尝试捕获,并发送JSON错误:

catch (error) {            
  res.status(400).json({
    success: false,
    message: 'This is ur message'
  })
}

现在,您可以为新错误更改“消息”:

catch (error) {            
  res.status(400).json({
    success: false,
    message: error.message
  })
}

这样,您可以将消息发送到前面,只需读取它即可。 。

let [functionRedux, {data : yourRedux , error}] = useFunctionMutation()
let msg = ""


if (yourRedux ?.success) {
  msg = yourRedux ?.message
} else {
  msg = error?.data.message
}

Now you have to create a Try and catch, and send a JSON error:

catch (error) {            
  res.status(400).json({
    success: false,
    message: 'This is ur message'
  })
}

Now, you can change the "message" for your new error:

catch (error) {            
  res.status(400).json({
    success: false,
    message: error.message
  })
}

With that, you can send the message to the front, and there just have to read it... For that, if u use Redux, can

let [functionRedux, {data : yourRedux , error}] = useFunctionMutation()
let msg = ""


if (yourRedux ?.success) {
  msg = yourRedux ?.message
} else {
  msg = error?.data.message
}

And with that, you can get the error to your front =)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文