Modernizing and simplifying an old node.js+mongoose code

Viewed 9

I am currently using an oAuth2.0 package of node.js and making API on express. I want to send response to the client side but this package contains some old codes. I'm not understanding this way.

var getAccessToken = function(token, callback) {

    tokenModel.findOne({
        accessToken: token
    }).lean().exec((function(callback, err, token) {

        if (!token) {
            console.error('Token not found');
        }

        callback(err, token);
    }).bind(null, callback));
};

Is there any simpler way to write this code? I dont get this bind, lean, exec, callback stuffs. I want this in similar way as below:

let getAccessToken = () =>{
let currentToken = tokenModel.findOne({
accessToken: token
})
if(!currentToken){
// Send a msg that "no token found" that i can catch in the API point and res.send() it as an error 
}else{
// Send a msg that "Token exists!" that i can catch in the API point and res.send() it as an error 
}
}

I'm not sure if this is the way to write it or if I'm doing it totally wrong, I really appreciate your help. Thank you.

0 Answers
Related