【问题标题】:"callback is not a function" Node.Js“回调不是函数”Node.Js
【发布时间】:2016-06-16 04:27:25
【问题描述】:

我有一个用 mysql 用 NodeJs 编写的项目,async.waterfall

我还实现了 async.waterfall 以避免我最近遇到的关于“回调不是函数”的问题

但问题依然存在。

这是我的 async.waterfall

async.waterfall([
    function (callback) {
        hold.getEntry(function(data){
            var ref = data.ref;
            id = data.id;
            var message = data.mess;
            json = JSON.parse(message);

            return callback(null, {'ref':ref, 'id':id, 'json':json});
        });
    },
    function (dataa, callback) {
        if(dataa.ref === null){
            callback(null);
        }else{
            hold.checkPositionCandidate(dataa.ref, dataa.id, dataa.json, function(dataaa){
                return callback(null, dataaa);
            });
        }
    },
    function(anoData, callback) {
        console.log(anoData);
        if(anoData === true){

             //the err is here
            hold.getVoterCount(id, json, function(votercount){
                if(votercount == 0){
                } else {
                    console.log('last function');
                }
            });
        } else {
        }
    }
], function (err, results) {
   // When finished execute this
});

这是我的 getVotercount 函数

function getVoterCount (id, callback){
    pool.getConnection(function(err, con){
        con.query("select total_voters as tv from pollwatcher_view where party_id = ?", [id], function(err, rows){
        setTimeout(function(){

            //this callback is not a function
            callback(null, {'count':rows[0].tv});
            console.log(rows);
        }, 2000);
        }); 
    });
}

我非常接近完成我的项目,但那个错误让我感到沮丧。请有人帮助我。

【问题讨论】:

  • 所以你用三个参数调用函数...
  • ...所以把hold.getVoterCount(id, json, function(votercount){...})改成hold.getVoterCount(id, function(votercount){..})
  • @elclanrs 是的,我在外面有一个 json 变量
  • @NikolayErmakov 你是我的救星!我没看到。谢谢!

标签: javascript node.js asynchronous callback


【解决方案1】:

你好像在打电话

hold.getVoterCount(id, json, function(votercount){
                if(votercount == 0){
                } else {
                    console.log('last function');
                }
            });

但您的 getVoterCount 函数仅使用 2 个预期参数定义。我建议尝试只传入 2 个参数:

hold.getVoterCount(id, function(votercount){
            if(votercount == 0){
            } else {
                console.log('last function');
            }
        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-09
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-14
    • 1970-01-01
    相关资源
    最近更新 更多