回调在NodeJ中不起作用

时间:2018-04-20 16:24:55

标签: javascript node.js

我正在尝试处理NodeJ中的异步调用。这是我的代码:

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');
    });

  });
}

getUserProfile函数中使用的回调有效,但它永远不会到达console.log("Call back Worked");。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

未调用回调,因为您对user_id, username, hashPass, res, function的调用传递了5个参数getUserProfile(user_id, username, hashPass, res, function (err, profile) {...} ,而该函数只需要4个:

function getUserProfile(user_id, username, hashPass, callback)

功能:

res

要使其有效,只需删除getUserProfile来电中的hiiiii参数。