节点:已经调用了回调

时间:2018-04-04 14:05:02

标签: node.js callback

我知道这里有一些重复的问题。但是,我不知道如何解决这个问题。我的代码是这样的:

/**
 * DEFINE
 * @connection_kill
 */
apiRoutes.post('/balancer_connection_drop', function(req, res) {
    /* BALANCER - connection kill */
    async.auto({
        decrypt_kill: function(callback, drop, user, stream) {
            /* DROP - decrypt */
            try {
                /* DEFINE - drop */
                var drop = JSON.parse(decrypt(Object.keys(req.body)[0], encryption));

                /* DROP - user & stream */
                callback(null, drop['drop'], drop['user'], drop['stream']);
            } catch(s) {
                /* CLOSE - connection */
                res.end();
            }
        },
        remove_connection: ['decrypt_kill', function(results, callback) {
            /* DROP - balancer || user & stream */
            if ((results[Object.keys(results)[0]])[0] !== 'null') {
                /* DROP - balancer users */
                if (results[Object.keys(results)[0]][0] == 'balancer') {
                    console.log('callback users...');
                    /* DROP - balancer users */
                    pool.query('UPDATE live SET remove=?',[1], function(err, results) {
                        /* DROP - failed */
                        if (err) return callback('Could NOT Drop Users From Live Table!');
                    })
                } else {
                    console.log('callback user...');
                    /* DROP - balancer user */
                    pool.query('UPDATE live SET remove=? WHERE user=? AND stream=?',[1, results[Object.keys(results)[0]][1], results[Object.keys(results)[0]][2]], function(err, results) {
                        /* DROP - failed */
                        if (err) return callback('Could NOT Drop Users From Live Table!');
                    })
                }
            } else {
                console.log('failed to kill data empty...');
                return callback('Balancer Connection Kill Data Empty!');
            }
            console.log('callback...');
            callback();
        }]
    }, function(err, results) {
        /* CHECK - error */
        if (err) return res.send(encrypt(JSON.stringify({'status': 'false'}), encryption));
        /* DROP - success */
        res.send(encrypt(JSON.stringify({'status': 'true'}), encryption));
        /* END - connection */
        res.end();
    })
})

现在,当我调用此api C时,从节点获取此错误:

  

已经调用了回调

所以我输入代码console.log以查看两次调用的内容,我从console.log获取此内容,然后抛出两次调用的错误:

callback user...
callback...

所以我看到我犯了错误,我称之为两次,但我不知道如何重写上面的代码,因为它应该是这样,当调用回调用户...时。我需要去

function(err, results) {

函数并在该函数中将json返回给我的服务器api,但我不知道如何重写它。我尝试使用上面的代码在函数外部移动回调,但从不调用回调。

3 个答案:

答案 0 :(得分:2)

在pool中处理错误之后使用回调。在最后一个问题中使用callback()是使用callback(),其中pool.query是异步任务,并且在将其推送到libuv以处理异步任务之后,节点跳过该异步任务然后它在pool.query回调函数之前到达最后一个callback();所以一旦最后一个回调被调用,那么当你的代码在pool.query回调函数中找到它时你的查询就会抛出错误,即返回回调('无法从用户表中删除用户!');因为之前调用了回调,所以它已经调用了错误,因为已经调用了回调。

apiRoutes.post('/balancer_connection_drop', function(req, res) {
/* BALANCER - connection kill */
async.auto({
    decrypt_kill: function(callback, drop, user, stream) {
        /* DROP - decrypt */
        try {
            /* DEFINE - drop */
            var drop = JSON.parse(decrypt(Object.keys(req.body)[0], encryption));

            /* DROP - user & stream */
            callback(null, drop['drop'], drop['user'], drop['stream']);
        } catch(s) {
            /* CLOSE - connection */
            res.end();
        }
    },
    remove_connection: ['decrypt_kill', function(results, callback) {
        /* DROP - balancer || user & stream */
        if ((results[Object.keys(results)[0]])[0] !== 'null') {
            /* DROP - balancer users */
            if (results[Object.keys(results)[0]][0] == 'balancer') {
                console.log('callback users...');
                /* DROP - balancer users */
                pool.query('UPDATE live SET remove=?',[1], function(err, results) {
                    /* DROP - failed */
                    if (err){ return callback('Could NOT Drop Users From Live Table!');}

                  return callback();
                })
            } else {
                console.log('callback user...');
                /* DROP - balancer user */
                pool.query('UPDATE live SET remove=? WHERE user=? AND stream=?',[1, results[Object.keys(results)[0]][1], results[Object.keys(results)[0]][2]], function(err, results) {
                    /* DROP - failed */
                    if (err){ return callback('Could NOT Drop Users From Live Table!');}

                       return callback();
                })
            }
        } else {
            console.log('failed to kill data empty...');
            return callback('Balancer Connection Kill Data Empty!');
        }

    }]
}, function(err, results) {
    /* CHECK - error */
    if (err) return res.send(encrypt(JSON.stringify({'status': 'false'}), encryption));
    /* DROP - success */
    res.send(encrypt(JSON.stringify({'status': 'true'}), encryption));
    /* END - connection */
    res.end();
})
 })

答案 1 :(得分:0)

return放在pool.query来电前面。您的代码执行落在if statement到,

...
console.log('callback...');
callback();

FYI,

你已经有if-else捕获所有路径,为什么你有这个?

...
console.log('callback...');
callback();

答案 2 :(得分:0)

    remove_connection: ['decrypt_kill', function(results, callback) {
        /* DROP - balancer || user & stream */
        if ((results[Object.keys(results)[0]])[0] !== 'null') {
            /* DROP - balancer users */
            if (results[Object.keys(results)[0]][0] == 'balancer') {
                console.log('callback users...');
                /* DROP - balancer users */
                pool.query('UPDATE live SET remove=?',[1], function(err, results) {
                    /* DROP - failed */
                    if (err) return callback('Could NOT Drop Users From Live Table!');
                })
            } else {
                console.log('callback user...');
                /* DROP - balancer user */
                pool.query('UPDATE live SET remove=? WHERE user=? AND stream=?',[1, results[Object.keys(results)[0]][1], results[Object.keys(results)[0]][2]], function(err, results) {
                    /* DROP - failed */
                    if (err) return callback('Could NOT Drop Users From Live Table!');
                })
            }
        } else {
            console.log('failed to kill data empty...');
            return callback('Balancer Connection Kill Data Empty!');
        }
        console.log('callback...');
        callback();
    }]

由于pool.query是异步操作,callback()之后的console.log('callback...');将在完成之前被调用。如果他们失败了,他们会再次打电话。

我删除了最后一次callback()来电,并在每次异步调用运行后调用它,如下所示:

    remove_connection: ['decrypt_kill', function(results, callback) {
        /* DROP - balancer || user & stream */
        if ((results[Object.keys(results)[0]])[0] !== 'null') {
            /* DROP - balancer users */
            if (results[Object.keys(results)[0]][0] == 'balancer') {
                console.log('callback users...');
                /* DROP - balancer users */
                pool.query('UPDATE live SET remove=?',[1], function(err, results) {
                    /* DROP - failed */
                    if (err) return callback('Could NOT Drop Users From Live Table!');
                    callback();
                })
            } else {
                console.log('callback user...');
                /* DROP - balancer user */
                pool.query('UPDATE live SET remove=? WHERE user=? AND stream=?',[1, results[Object.keys(results)[0]][1], results[Object.keys(results)[0]][2]], function(err, results) {
                    /* DROP - failed */
                    if (err) return callback('Could NOT Drop Users From Live Table!');
                    callback();
                })
            }
        } else {
            console.log('failed to kill data empty...');
            return callback('Balancer Connection Kill Data Empty!');
        }
    }]