NodeJS Callback不是函数Async瀑布

时间:2016-12-11 22:17:50

标签: javascript mysql node.js async.js

我一直在编写一个简单的服务器,虽然在我的函数头中有一个回调参数,但我遇到了一个问题,我收到了一个回调未定义的错误。我在Google上看过的每个例子都是没有回调参数的人,但我似乎有一个?抛出的错误是TypeError:callback不是函数。

代码:

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

1 个答案:

答案 0 :(得分:1)

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

pool.query中的内部函数需要2个参数:errrows。所以在返回callback时,它需要2个参数。由于第一个参数为err,因此在callback中使用null,因为只需要调用if(!err)条件就满足了。{/ p>