我的驗證令牌函數返回undefined而不是true,issuetoken函數發出令牌,驗證令牌函數驗證發出的令牌。我的函數返回undefined,我該如何解決
var jwt = require('jsonwebtoken'); //import the jwt mmodule
var secret = process.env.JWT_SECRET || "david"; // secret question
//issue token function
issuetoken = function (username) {
var token = jwt.sign({
username,
exp: Math.floor(Date.now()/1000) + (60 * 60),
}, secret);
return token;
}
//this function verifies the token, it return true if verification is succesfull and return false if not successfull
verifytoken = function (token, secret) {
jwt.verify(token, secret, function (err, vt) {
if (err) {
console.log(err);
return false;
}
else {
return true;
}
});
}
var token = issuetoken("david");// issue the token with username david
var a = verifytoken(token, secret); //verification of token
console.log(a); //it returns undefined instead of true
可能重複[如何從異步調用返回響應?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an -asynchronous-call) – Frxstrem