[英]Property 'email' does not exist on type 'string'
I am very new to typescrypt, and I'm currently in the process of migrating a project from JS to TS.我对 typecrypt 很陌生,目前正在将项目从 JS 迁移到 TS。 While I was changing some things on my server endpoints, an error appeared:
当我在我的服务器端点上更改一些东西时,出现了一个错误:
Property 'email' does not exist on type 'string | JwtPayload'
This is my code so far:到目前为止,这是我的代码:
try {
const token = await TokenController.getTokenFromRequest(req);
const decoded = jwt.decode(token);
const user = await AuthController.getUserByMail(decoded.username); // <- Error appears here
res.json(user);
} catch (error: any) {
ErrorController.errorCallback(error, res);
}
I know that the error happens because I'm trying to access a property that string
doesn't have.我知道发生错误是因为我试图访问
string
没有的属性。 Any suggestions?有什么建议么?
Create global.d.ts:创建 global.d.ts:
// global.d.ts
declare module "jsonwebtoken" {
export interface JwtPayload {
//make optional so when you try to decode stuff other than user it will show it can be undefined
email?: string;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.