JavaScript Joi错误,类型上不存在该属性。

huangapple go评论112阅读模式
英文:

JavaScript Joi error Property does not exist on type

问题

我正在尝试使用Joi验证我的API中的req.body

> 类型上不存在的属性

这是我的代码:

  1. const idScheme = Joi.string().required();
  2. const updatedBioScheme = Joi.object({
  3. bio: Joi.string()
  4. .min(3)
  5. .required(),
  6. });
  7. export async function
  8. updatedBio(req: Request, res: Response,) {
  9. try {
  10. const uid = req.query.uid;
  11. const {err, val} = updatedBioScheme.validate(req.body);
  12. const {error, value} = idScheme.validate(uid);
  13. if (error) {
  14. res.status(400).json({error: "Invalid uid parameter"});
  15. return;
  16. } else if (err) {
  17. return res.status(400).json(err);
  18. }
  19. const userId = value;
  20. const {bio}= val;
  21. const inject = myContainer.get<ProfileServicesRepository>(TYPES.ProfileServices);
  22. const data = await inject.updateBio(userId, bio);
  23. if (data == undefined) {
  24. return res.status(400).json({error: "User not available yet!!"});
  25. }
  26. return res.status(200).json({data, message: "Sucessful"});
  27. } catch (error) {
  28. return res.status(400).send({error, message: "error"});
  29. }
  30. }

我在这里获得错误:const {err, val} = updatedBioScheme.validate(req.body);

JavaScript Joi错误,类型上不存在该属性。

英文:

I am trying to validate my req.body from my API using Joi:

> Property does not exist on type

Here is my code:

  1. const idScheme = Joi.string().required();
  2. const updatedBioScheme = Joi.object({
  3. bio: Joi.string()
  4. .min(3)
  5. .required(),
  6. });
  7. export async function
  8. updatedBio(req: Request, res: Response,) {
  9. try {
  10. const uid = req.query.uid;
  11. const {err, val} = updatedBioScheme.validate(req.body);
  12. const {error, value} = idScheme.validate(uid);
  13. if (error) {
  14. res.status(400).json({error: "Invalid uid parameter"});
  15. return;
  16. } else if (err) {
  17. return res.status(400).json(err);
  18. }
  19. const userId = value;
  20. const {bio}= val;
  21. const inject = myContainer.get<ProfileServicesRepository>(TYPES.ProfileServices);
  22. const data = await inject.updateBio(userId, bio);
  23. if (data == undefined) {
  24. return res.status(400).json({error: "User not available yet!!"});
  25. }
  26. return res.status(200).json({data, message: "Sucessful"});
  27. } catch (error) {
  28. return res.status(400).send({error, message: "error"});
  29. }
  30. }

I am getting the error from this const {err, val} = updatedBioScheme.validate(req.body);

JavaScript Joi错误,类型上不存在该属性。

答案1

得分: 1

我找到了错误,所以我只需将<T>赋给一个值。

  1. const {error: err, value: val} = updatedBioScheme.validate(req.body);
英文:

i figure out the error, so i just <T> to a value

  1. const {error: err, value: val} = updatedBioScheme.validate(req.body);

答案2

得分: 0

根据 Joi 文档,你有 valueerror。而你尝试提取 errval,但这两个属性在对象上不存在,TypeScript 已经知道这一点。

英文:

According to Joi doc you have value, error. Instead you try to extract err and val which are non-existent on the object and TypeScript knows it.

huangapple
  • 本文由 发表于 2023年7月20日 19:25:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729369.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定