简体   繁体   中英

getting undefined when using typescript

I'm trying to implement oauth2-server using typescript. And need to define the model used and assigning it in the constructor of the Oauth2Server. But getting undefined model at runtime.

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

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. But can not figure out why the model is undefined when I use typescript.

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.

You can check return false to check it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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