mysql - 如何从 Node.js 中运行 Mysql Select 的函数返回值

标签 mysql node.js

我正在运行一个简单的程序进行长轮询,以检查数据库中是否发生任何更改,并将其发送到 Node.js 中的浏览器。我知道如何使用回调函数,但我需要从 program.query 函数返回一个值,因为我将在递归函数中使用它。感谢您的帮助。

var server=http.createServer(function(req,res){
    program.run(res, 5);
});

server.on('listening',function(){
    console.log('ok, server is running');
});

server.listen(9000);


var program = {

    run: function(res, id, old){

        if(old == undefined){
            // I need a value from database to be returned to old variable
            old = program.query(res, id /*, function(){res.end('there') }*/);
        }

        clearTimeout(tt);
        var tt = setTimeout( function(){

            // I need a value from database to be returned to new_ variable
            var new_ = program.query(res, id /*, function(){res.end('there') }*/);

            if(new_ != old){
                // ... Send Response with the change to browser
            }
            else{ // no change in database, so run again
                old = new_;
                program.run(res, id, old);
            }

        }, 2000);    

    },

    query: function(res, id /*,callback*/){

        connection.query('SELECT table1.column1 FROM table1 WHERE table1.id ="'+id+'"', function(err, rows, fields){


            //callback(res,id,rows[0].column1); // I don't want this solution
            return rows[0].column1; // Not working..

        });


    }

};

最佳答案

只需在回调中递归调用即可。它看起来和听起来一样:

var program = {

    run: function(res, id, old){

        if(old == undefined){
            program.query(res, id , function(res,id,result){
                old = result;

                clearTimeout(tt);
                var tt = setTimeout( function(){
                    program.query(res, id , function(res,id,new_){
                        if(new_ != old){
                            // ... Send Response with the change to browser
                        }
                        else{ // no change in database, so run again
                            old = new_;
                            program.run(res, id, old);
                        }
                        // not sure where you want to put the res.end()
                        // since I don't really know your logic.
                    });

                }, 2000);
            });
        }
        else {
            // This code is of course identical to the code above
            // so you can refactor it out into its own function.

            clearTimeout(tt);
            var tt = setTimeout( function(){
                program.query(res, id , function(res,id,new_){
                    if(new_ != old){
                        // ... Send Response with the change to browser
                    }
                    else{ // no change in database, so run again
                        old = new_;
                        program.run(res, id, old);
                    }
                    // not sure where you want to put the res.end()
                    // since I don't really know your logic.
                });

            }, 2000);
        }
    },

    query: function(res, id, callback){

        connection.query('SELECT table1.column1 FROM table1 WHERE table1.id ="'+id+'"', function(err, rows, fields){


            callback(res,id,rows[0].column1);    
        });


    }

};

关于mysql - 如何从 Node.js 中运行 Mysql Select 的函数返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23621183/

相关文章:

node.js - Azure Functions 未执行任何操作就超时

mysql - Sequelize - 原始 :true and raw:false 和有什么区别

javascript - 进行多个异步调用而不相互依赖

javascript - Node 串行端口模块无法与子进程 fork() 一起使用

python - MySQLdb 模块是否支持准备好的语句?

mysql - 在 MySQL 中有条件地加入

MYSQL 访问被拒绝用户 'root' @'localhost'

php - SQL内连接性能问题

javascript - 两个循环并且未连接到Mysql

javascript - 如果我打算使用 Node,那么直接使用像 Zappa 这样的 CoffeeScript 框架是错误的吗?