【问题标题】:getting undefined when using typescript使用打字稿时变得未定义
【发布时间】:2019-01-10 04:11:54
【问题描述】:

我正在尝试使用打字稿实现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

欢迎所有建议

最好的问候

【问题讨论】:

    标签: node.js typescript oauth-2.0


    【解决方案1】:

    我看到一个案例返回undefined,它在try 内部,抛出错误并且你捕获它但不返回任何东西。

    您可以查看return false查看。

    【讨论】:

      猜你喜欢
      • 2022-10-06
      • 2019-04-30
      • 1970-01-01
      • 1970-01-01
      • 2016-11-06
      • 2016-09-05
      • 2016-12-13
      • 2020-02-12
      • 2017-12-22
      相关资源
      最近更新 更多