Reputation: 21
I've been writing a simple server, and have come across an issue where I get a callback undefined error, despite having a callback parameter in my function header. Every example I've seen on Google has been people not having a callback parameter, but I seem to have one? The error that is thrown is TypeError: callback is not a function.
Code:
app.post("/api/info/", function(req, res){
async.waterfall([
function(callback){
debugger;
jwt.verify(req.body.jwt, secret.secret, {ignoreExpiration: false},
function(err, decoded){
console.log(err);
console.log(decoded.uuid);
debugger;
if(err) {
debugger;
console.log("error1: " + err);
debugger;
res.json({
status: 403
});
res.end();
callback("JWT auth failed", null);
return;
}
debugger;
callback(null, decoded.uuid);
});
},
function(err, token, callback){ //query db for user uuid
debugger;
pool.query("SELECT * FROM users WHERE INSTR (uuid," + "'" + token + "') > 0", function(err, rows){
if(err) console.log(err);
debugger;
callback(rows); //where it errors
});
},
function(rows, callback){ //get user's school name
debugger;
var schoolId = rows[0].chapter_uuid;
pool.query("SELECT * FROM chapters WHERE INSTR (uuid," + "'" + schoolId + "') > 0", function(err, result){
debugger;
callback(rows, result[0].school);
});
},
function(rows, chapter, callback){ //return db results to user in json
debugger;
var canMakeEvent = 0;
if(rows[0].is_board){
canMakeEvent = 1;
}
var info = {
firstName: rows[0].first_name,
lastName: rows[0].last_name,
email: rows[0].email,
chapter: chapter,
isBoard: canMakeEvent,
boardPosition: rows[0].board_position
};
res.json({
status: 200,
info: info
});
res.end();
debugger;
callback(null);
}
], function(err){console.log(err);});
});
Upvotes: 0
Views: 772
Reputation: 2839
function(err, token, callback){ //query db for user uuid
debugger;
pool.query("SELECT * FROM users WHERE INSTR (uuid," + "'" + token + "') > 0", function(err, rows){
if(err) console.log(err);
debugger;
callback(null,rows); //where it errors
});
},
The inner function in pool.query
takes 2 parameters: err
and rows
. So in the returning callback
, it requires 2 parameters. Since 1st parameter is err
, in callback
use null
since it need to be called only if(!err)
condition is satisfied.
Upvotes: 1