简体   繁体   中英

NodeJS Callback not a function Async waterfall

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM