[英]getting undefined when using typescript
I'm trying to implement oauth2-server using typescript. 我正在尝试使用Typescript实现oauth2-server 。 And need to define the model used and assigning it in the constructor of the Oauth2Server.
并且需要定义所使用的模型并将其分配给Oauth2Server的构造函数。 But getting
undefined
model at runtime. 但是在运行时获取
undefined
模型。
I defined a set of functions like this 我定义了这样的一组函数
async function getClient(clientId:string, clientSecret:string) {
try {
let dbClient = await OAuthClient.findById(clientId)
console.log(dbClient)
let client = toClientDTO(dbClient);
if(!client){
console.log("client not found");
return false
}
if(clientSecret && clientSecret !== client.secret){
throw new Error("client not found: 'secret error'");
}
console.log("Client return value", client);
return client;
} catch(e) {
console.log("get client err", e);
}
}
and exporting them like this: 然后像这样导出它们:
export const model = {
getAccessToken: getAccessToken,
getClient: getClient,
getRefreshToken: getRefreshToken,
getUser: getUser,
saveToken: saveToken,
validateScope: validateScope,
verifyScope: verifyScope,
}
I am using the model here: 我在这里使用模型:
import Oauth2Server from 'oauth2-server';
import {model} from './model';
console.log(model);
const oauth = new Oauth2Server({
model: model,
requireClientAuthentication: {password: false}
});
export default oauth
but when I call functions on the oauth object.it complains that the model is undefined 但是当我在oauth对象上调用函数时,它抱怨模型未定义
import oauth from './oauth';
export default (app:Application) => {
app.all('/oauth/token', (req:Request,res:Response) => {
var request = new OAuthRequest(req);
var response = new OAuthResponse(res);
oauth
.token(request,response)
.then(function(token) {
return res.json(token)
}).catch(function(err){
return res.status(500).json(err)
})
});
}
I can get it to work using mostly the same setup and code using regular javascript. 我可以使用与常规javascript大致相同的设置和代码来使其正常工作。 But can not figure out why the model is
undefined
when I use typescript. 但是无法弄清楚为什么我使用打字稿时
undefined
模型。
All suggestions are welcome 欢迎所有建议
Best regards 最好的祝福
I see one case return undefined
, It is inside try
, throw error and you catch it but don't return anything. 我看到一种情况返回
undefined
,它在try
,引发错误,您捕获了它,但未返回任何内容。
You can check return false
to check it. 您可以检查
return false
进行检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.