英文:
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);
英文:
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);
答案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 文档,你有 value
和 error
。而你尝试提取 err
和 val
,但这两个属性在对象上不存在,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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论