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

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

JavaScript Joi error Property does not exist on type

问题

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

> 类型上不存在的属性

这是我的代码:

const idScheme = Joi.string().required();
const updatedBioScheme = Joi.object({
  bio: Joi.string()
      .min(3)
      .required(),
});

export async function
updatedBio(req: Request, res: Response,) {
  try {
    const uid = req.query.uid;
    const {err, val} = updatedBioScheme.validate(req.body);
    const {error, value} = idScheme.validate(uid);
    if (error) {
      res.status(400).json({error: "Invalid uid parameter"});
      return;
    } else if (err) {
      return res.status(400).json(err);
    }
    const userId = value;
    const {bio}= val;
    const inject = myContainer.get<ProfileServicesRepository>(TYPES.ProfileServices);
    const data = await inject.updateBio(userId, bio);
    if (data == undefined) {
      return res.status(400).json({error: "User not available yet!!"});
    }
    return res.status(200).json({data, message: "Sucessful"});
  } catch (error) {
    return res.status(400).send({error, message: "error"});
  }
}

我在这里获得错误: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:

const idScheme = Joi.string().required();
const updatedBioScheme = Joi.object({
  bio: Joi.string()
      .min(3)
      .required(),
});

export async function
updatedBio(req: Request, res: Response,) {
  try {
    const uid = req.query.uid;
    const {err, val} = updatedBioScheme.validate(req.body);
    const {error, value} = idScheme.validate(uid);
    if (error) {
      res.status(400).json({error: "Invalid uid parameter"});
      return;
    } else if (err) {
      return res.status(400).json(err);
    }
    const userId = value;
    const {bio}= val;
    const inject = myContainer.get<ProfileServicesRepository>(TYPES.ProfileServices);
    const data = await inject.updateBio(userId, bio);
    if (data == undefined) {
      return res.status(400).json({error: "User not available yet!!"});
    }
    return res.status(200).json({data, message: "Sucessful"});
  } catch (error) {
    return res.status(400).send({error, message: "error"});
  }
}

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

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

答案1

得分: 1

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

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

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

 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:

确定