Javascript socket.io和节点回调函数不工作

Javascript socket.io和节点回调函数不工作,javascript,node.js,asynchronous,callback,socket.io,Javascript,Node.js,Asynchronous,Callback,Socket.io,我很难掌握socket.io中的异步回调和变量存储。我希望回调函数中的代码在所有查询完成后执行。但是,我不知道在哪里放置callback()方法调用,以便在完成所有操作后执行。我将非常感谢任何和所有的帮助 //The code below queries a database and stores the information in a json object. var mysql = require('mysql') var io = require('socket.io').lis

我很难掌握socket.io中的异步回调和变量存储。我希望回调函数中的代码在所有查询完成后执行。但是,我不知道在哪里放置callback()方法调用,以便在完成所有操作后执行。我将非常感谢任何和所有的帮助

//The code below queries a database and stores the information in a json object.
var mysql = require('mysql')
    var io = require('socket.io').listen(3000)
    var db = mysql.createConnection({     
        host: '',
        user: '',
        password: '',
        database: '',
        port: 3306,
    })
    db.connect(function(err){
        if (err) console.log(err)
    })

console.log(1);
io.sockets.on('connection', function(socket){
   socket.on('key', function(value){//client side has a onclick() function that emits 'key'
        console.log(2);
        var total = {};
        var personalTable = []
        var liwcTable = []
        var id = value;

        help(total, function(total) {
            console.log(4);
            console.log("total = " + JSON.stringify(total));
            socket.emit('total', total);/emits to client
        });

        function help(total, callback) {
            console.log(3);
            db.query('SELECT * FROM `a` WHERE `userId` =' + id)
                .on('result', function(data){
                    liwcTable.push(data)
                })
                .on('end', function(){
                    total["initial liwcTable"] = liwcTable;
                })
            db.query('SELECT * FROM `b`  WHERE `userId` =' + id)
                .on('result', function(data){
                    personalTable.push(data)
                })
                .on('end', function(){
                    total['personalTable'] = personalTable;
                }) 
            callback(total)//needs to be executed after the queries are done.
         }
    })    
})

在查询有机会完成之前,代码进入回调方法。我也不明白在查询回调的范围有限的情况下,如何更新json对象“total”。

您有很多解决方案可以在所有想要的操作之后触发回调。 例如,您可以在每个查询之后创建一个调用的单例,它将触发最终回调

function help(total, callback) {

    var nbEndedQueries = 0,
        amountOfQueries = 2;//nb of linked queries
    function singleton() {

        //increments the nbEndedQueries variable and tests if the max is reached
        if(++nbEndedQueries >= amountOfQueries)
            callback();
    }


    db.query(... //query calling)
        .on('result', ...//some behaviours
            singleton();
        )
    db.query(... //query calling)
        .on('result', ...//some behaviours
            singleton();
        )
    //...

}
另一个解决办法是使用承诺。 许多模块,如Q或ECMA6 polyfill,都为您提供了这一功能,这是非常棒的

带有ecm6 polyfill的样品

//promisification of the query method
function query(string) {
    return new Promise(function(resolve, reject) {

        db.query(string)
            .on('result', resolve)
            //i supposed an error event exist
            .on('error', reject);

    })
}

//and now the usage
function help() {

    //basic method
    //first query calling
    query('your query string')
        .then(function(result) {
            //behaviour

            //second query calling, the result will send to the next 'then' statement
            return query('second query');
        })
        .then(function() {
            //behaviour


            //at this point, all queries are finished
            callback() ;
        });
}


您有许多解决方案可以在所有想要的操作之后触发回调。 例如,您可以在每个查询之后创建一个调用的单例,它将触发最终回调

function help(total, callback) {

    var nbEndedQueries = 0,
        amountOfQueries = 2;//nb of linked queries
    function singleton() {

        //increments the nbEndedQueries variable and tests if the max is reached
        if(++nbEndedQueries >= amountOfQueries)
            callback();
    }


    db.query(... //query calling)
        .on('result', ...//some behaviours
            singleton();
        )
    db.query(... //query calling)
        .on('result', ...//some behaviours
            singleton();
        )
    //...

}
另一个解决办法是使用承诺。 许多模块,如Q或ECMA6 polyfill,都为您提供了这一功能,这是非常棒的

带有ecm6 polyfill的样品

//promisification of the query method
function query(string) {
    return new Promise(function(resolve, reject) {

        db.query(string)
            .on('result', resolve)
            //i supposed an error event exist
            .on('error', reject);

    })
}

//and now the usage
function help() {

    //basic method
    //first query calling
    query('your query string')
        .then(function(result) {
            //behaviour

            //second query calling, the result will send to the next 'then' statement
            return query('second query');
        })
        .then(function() {
            //behaviour


            //at this point, all queries are finished
            callback() ;
        });
}


您有许多解决方案可以在所有想要的操作之后触发回调。 例如,您可以在每个查询之后创建一个调用的单例,它将触发最终回调

function help(total, callback) {

    var nbEndedQueries = 0,
        amountOfQueries = 2;//nb of linked queries
    function singleton() {

        //increments the nbEndedQueries variable and tests if the max is reached
        if(++nbEndedQueries >= amountOfQueries)
            callback();
    }


    db.query(... //query calling)
        .on('result', ...//some behaviours
            singleton();
        )
    db.query(... //query calling)
        .on('result', ...//some behaviours
            singleton();
        )
    //...

}
另一个解决办法是使用承诺。 许多模块,如Q或ECMA6 polyfill,都为您提供了这一功能,这是非常棒的

带有ecm6 polyfill的样品

//promisification of the query method
function query(string) {
    return new Promise(function(resolve, reject) {

        db.query(string)
            .on('result', resolve)
            //i supposed an error event exist
            .on('error', reject);

    })
}

//and now the usage
function help() {

    //basic method
    //first query calling
    query('your query string')
        .then(function(result) {
            //behaviour

            //second query calling, the result will send to the next 'then' statement
            return query('second query');
        })
        .then(function() {
            //behaviour


            //at this point, all queries are finished
            callback() ;
        });
}


您有许多解决方案可以在所有想要的操作之后触发回调。 例如,您可以在每个查询之后创建一个调用的单例,它将触发最终回调

function help(total, callback) {

    var nbEndedQueries = 0,
        amountOfQueries = 2;//nb of linked queries
    function singleton() {

        //increments the nbEndedQueries variable and tests if the max is reached
        if(++nbEndedQueries >= amountOfQueries)
            callback();
    }


    db.query(... //query calling)
        .on('result', ...//some behaviours
            singleton();
        )
    db.query(... //query calling)
        .on('result', ...//some behaviours
            singleton();
        )
    //...

}
另一个解决办法是使用承诺。 许多模块,如Q或ECMA6 polyfill,都为您提供了这一功能,这是非常棒的

带有ecm6 polyfill的样品

//promisification of the query method
function query(string) {
    return new Promise(function(resolve, reject) {

        db.query(string)
            .on('result', resolve)
            //i supposed an error event exist
            .on('error', reject);

    })
}

//and now the usage
function help() {

    //basic method
    //first query calling
    query('your query string')
        .then(function(result) {
            //behaviour

            //second query calling, the result will send to the next 'then' statement
            return query('second query');
        })
        .then(function() {
            //behaviour


            //at this point, all queries are finished
            callback() ;
        });
}