我正在尝试处理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");
。有什么想法吗?
答案 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
参数。