返回null mysql nodejs

时间:2017-06-06 12:29:53

标签: javascript mysql node.js express

请查看dbfn.js文件

/*database function file;*/

var db = require('./connection');

function check_conn(){
    if(db){
        console.log('We are connected to Database server'.bgGreen);
        return true;
    }else{
        console.log('We are not connected to DataBase server'.bgRed);
        return false;
    }
}

module.exports = {

    testCon : function(){
        if(check_conn() === true){
            return 'We are connected to DB';
        }else return 'We are not connected to DB';
    },

    get_user : function(){
        db.query('SELECT * FROM users',function(err,results,fields){
            if(err){
                console.log(err)
                return err;
            }
            if(results){
                console.log(results);
                return results;
            }
            console.log('nothing...');
        });
    }
};

现在,看看routes.js文件

var db = require('./dbfn');

module.exports = function(app){

    /*
        Routing starts here
    */

    //root route
    app.get('/', function(req, res){
        res.render('index.ejs');
    });

    //test route
    app.get('/test',function(req,res){

        var x = db.get_user();
        console.log(x);
        res.writeHead(200, {'Content-Type': 'text/json'});
        res.json(x);
    });



    //default it should stay at bottom
    app.get('*',function (req, res) {
        //res.redirect('/');
        res.send(404);
    });
}
dbfn文件上的console.log从MySQL数据库写入完整对象但是当我在routes.js文件上将其作为var x it null调用时,我真的不明白我实际上是如何克服它的。是的,我是NodeJs的新生

感谢。

2 个答案:

答案 0 :(得分:1)

您无法从同步函数内的异步调用返回。相反,您需要将回调传递给get_user(),如下所示:

<强> dbfn.js

var db = require('./connection');

function check_conn(){
    if(db){
        console.log('We are connected to Database server'.bgGreen);
        return true;
    }else{
        console.log('We are not connected to DataBase server'.bgRed);
        return false;
    }
}

module.exports = {

    testCon : function(){
        if(check_conn() === true){
            return 'We are connected to DB';
        }else return 'We are not connected to DB';
    },

    get_user : function(cb){
        db.query('SELECT * FROM users',function(err,results,fields){
            if(err){
                console.log(err)
                cb(err);
            }
            if(results){
                console.log(results);
                cb(null, results);
            }
            console.log('nothing...');
        });
    }

};

<强> routes.js

var db = require('./dbfn');

module.exports = function(app){

    /*
        Routing starts here
    */

    //root route
    app.get('/', function(req, res){
        res.render('index.ejs');
    });

    //test route
    app.get('/test',function(req,res){

        db.get_user(function(err, response) {
            if(err) {
                // handle error
            }
            else {
                var x = response;
                console.log(x);
                res.writeHead(200, {'Content-Type': 'text/json'});
                res.json(x);
            }
        });

    });

    //default it should stay at bottom
    app.get('*',function (req, res) {
        //res.redirect('/');
        res.send(404);
    });
}

现在,回调中的代码只会在数据库查询完成后触发。

答案 1 :(得分:0)

使用回调更新您的查询功能:

get_user : function(callback){
        db.query('SELECT * FROM users',function(err,results,fields){
            if(err){
                console.log(err)
                callback(err);
            }
            if(results){
                console.log(results);
                callback(results;
            }
            console.log('nothing...');
        });
    }

然后在您的路线中接下来检查回调函数中的结果

app.get('/test',function(req,res){

        db.get_user(function(result){
         console.log(result);
     });
 });