0

I was trying to get jwtauthtoken from user and passing it to a getuserId() which was imported in that js file. But i was getting undefined value instead of decoded id that was returned by the function.

In the getUserId() it displays the decoded token

My console output:

user id 5f68efb234a7656

On get request : undefined

Anyone can help to me resolve the problem.

personalDetailsController.js

module.exports.personaldetail_get = async (req,res) => {

    const token = req.cookies.jwtauthtoken;

        let userId = await getUserId(token);
        console.log("On get request : "+ userId); // output On get request : undefined

    res.render('Candidate/personal', { csrfToken: req.csrfToken() });

}

getUserId.js

module.exports.getUserId =  (tokenid) => {

    const token = tokenid;

    try{

        if (token) {

            jwt.verify(token,'2308199919990823', async (err, decodedToken) => {
                if(err){
                    console.log(err);
                    return null;
                } else {
                    console.log("user id " + decodedToken.id); // Output user id 5f68efb234a7656
                    return decodedToken.id;
                }

            });
        } else {
            return null;
        }

    }
    catch(err)
    {
        console.log(err)
        return null;
    }

}
4
  • Hi, you forgot to add return at the function call jwt.verify... Commented Sep 18, 2020 at 7:33
  • if (token) the function doesn't return anything. Commented Sep 18, 2020 at 7:36
  • Does this answer your question? How do I return the response from an asynchronous call? Commented Sep 18, 2020 at 7:37
  • I tried but now also it returns undefined Commented Sep 18, 2020 at 7:41

2 Answers 2

3

const decodedToken = await getUserId(token) means two things :

  • getUserId must return a Promise.
  • decodedToken is given by the resolution of this Promise.
getUserId = tokenid => {
    return new Promise(resolve => {
        jwt.verify(tokenid, '2308199919990823', (err, decodedToken) => resolve(decodedToken))
    })
}
Sign up to request clarification or add additional context in comments.

Comments

1

you forgot the add a return on your function call...

Also the try catch block you have will not work since your verify function is asynchronous...you need to wrap this in a Promise...

module.exports.getUserId =  (tokenid) => {

    const token = tokenid;

    if (token) {
        return new Promise((reject, resolve) => {
            jwt.verify(token,'2308199919990823', async (err, decodedToken) => {
                if(err){
                    console.log(err);
                    return null;
                } else {
                    console.log("user id " + decodedToken.id); // Output user id 5f68efb234a7656
                    return resolve(decodedToken.id);
                }

            });
        });
    }
}

4 Comments

This will only return jwt.verify which is an asynchronous function. It will not return the value of the token, which is the expected result. Downvoted
Yeah sorry, i had just realized that this needs to be wrapped in a Promise for it to work properly
Made the edit, please check and verify...it should return a Promise of a token...thanks
Fair enough, canceled my downvote. But you should reject() instead of return null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.