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

时间:2014-05-13 00:50:43

标签: 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..

        });


    }

};

1 个答案:

答案 0 :(得分:2)

只需在回调中递归调用它。它看起来确实如此:

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


    }

};