繁体   English   中英

使用打字稿时变得不确定

[英]getting undefined when using typescript

我正在尝试使用Typescript实现oauth2-server 并且需要定义所使用的模型并将其分配给Oauth2Server的构造函数。 但是在运行时获取undefined模型。

我定义了这样的一组函数

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);

    }
}

然后像这样导出它们:

export const model =  {
    getAccessToken: getAccessToken,
    getClient: getClient,
    getRefreshToken: getRefreshToken,
    getUser: getUser,
    saveToken: saveToken,
    validateScope: validateScope,
    verifyScope: verifyScope,
}

我在这里使用模型:

import Oauth2Server from 'oauth2-server';
import {model} from './model';
console.log(model);
const oauth = new Oauth2Server({
    model: model,
    requireClientAuthentication: {password: false}
});
export default oauth

但是当我在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)
      })
  });
}

我可以使用与常规javascript大致相同的设置和代码来使其正常工作。 但是无法弄清楚为什么我使用打字稿时undefined模型。

欢迎所有建议

最好的祝福

我看到一种情况返回undefined ,它在try ,引发错误,您捕获了它,但未返回任何内容。

您可以检查return false进行检查。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM