1

I am trying to handle asynchronous calls in NodeJs. Here is my code:

function getUserFromToken(token) {
  decodeJWT(token, function (err, data) {
    console.log("Call back Worked");
  })
}

function getUserProfile(user_id, username, hashPass, callback) {
  let sql = "SELECT first_name, last_name, username FROM User where user_id=? AND username=? AND password=?";

  db.query(sql, [user_id, username, hashPass], function (err, result) {
    if (err) callback(err, null);
    else callback(null, result)
  });
}

function decodeJWT(token, cb) {
  jwt.verify(token, config.secret, function (err, decoded) {

    if (err) res.status(401).send({auth: false, message: miscConstants.INVALID_TOKEN});

    const {user_id} = decoded;
    const {username} = decoded;
    const {hashPass} = decoded;

    getUserProfile(user_id, username, hashPass, res, function (err, profile) {

      cb(null,'yo');
    });

  });
}

The callback used in the getUserProfile function works but, it never reaches the console.log("Call back Worked");. Any ideas?

3
  • Do you actually have a function named callback somewhere? Commented Apr 20, 2018 at 16:48
  • handle error properly in decodeJWT you are calling getUserProfile , check if its throwing err. Its better to use promise you dont need to handle all errors Commented Apr 20, 2018 at 17:30
  • How did you find - The callback used in the getUserProfile function works ? Commented Apr 20, 2018 at 19:14

1 Answer 1

1

The callback is not being called because your call to getUserProfile is passing 5 parameters user_id, username, hashPass, res, function, while the function is expecting only 4:

getUserProfile(user_id, username, hashPass, res, function (err, profile) {...}

And the function:

function getUserProfile(user_id, username, hashPass, callback)

To make it work, just delete the parameter res in the getUserProfile call.

Sign up to request clarification or add additional context in comments.

Comments

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.