简体   繁体   English

使用打字稿时变得不确定

[英]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.

相关问题 我使用mocha与typescript得到一个未定义的错误 - I am getting an undefined error using mocha with typescript 使用 FieldValue 或 FirestoreAdminClient 时未定义 - Getting undefined when using FieldValue or FirestoreAdminClient 使用 Mongoose 更新许多时未定义 - Getting undefined when updating many using Mongoose 节点 Typescript:使用括号表示法时未定义 JSON - Node Typescript: JSON undefined when using bracket notation 在打字稿节点中从异步等待中获取未定义 - getting undefined from async await in typescript node 从JSON数据调用值时使用fetch()并获取“未定义” - Using fetch() and getting 'Undefined' when calling a value from the JSON data 在 nodejs 打字稿中扩展类时未定义的函数 - Undefined function when extends class in nodejs typescript 节点和 typescript:调用路由时属性未定义 - node and typescript: property undefined when call route 正文解析器不适用于打字稿,当我发送请求时,我在 request.body 中得到一个 undefined - Body-parser doesn't work with typescript, when I send a request, I'm getting an undefined in request.body Typescript 忽略未定义 {…undefined} 的传播时无法转译 - Typescript fails to transpile when leaving out spread of undefined {…undefined}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM