最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Connection.query to execute - Stack Overflow

programmeradmin2浏览0评论

Pretty sure this is a quite noobish node.js/callback question but I can't seem to find the proper code to make it run.

This is how I invoke my node-mysql code:

var utils = require('../../config/database/utils');

exports.getResults = function(callback) {   
  var query = "SELECT * FROM my_table"; 
  utils.exec(query, null, function(err, results){
    if(err){
      console.log(err);
      callback(true);
      return;
    }
    console.log(results);
    callback(false, results);
  });
};

Next is the utils file where I can't get the code work.

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

module.exports = {
getDBConnection: function() {
    pool.getConnection(function(err, connection){
        if(err){
            console.log(err);
            return;
        }
        return connection;
    });
},
endDBConnection: function(connection) {
    connection.end(function (err) {
        if(err) {
            console.log(err); 
            callback(true); 
            return; 
        }   
    });
},
exec: function(query, data, callback) {
    console.log(query);
    this.getDBConnection(function(err, connection){
        if(err){
            console.log('error');
        }
        console.log(connection);
        connection.query(query, data, function(err, results) {
            if(err) {
                callback(err);
            }
            callback(false, results);
        });
        this.endDBConnection(connection);
    });
}
}

Code is getting OK the the exec part since the console.log(query) logs the query. But after that, the code's not running, console.log(connection); doesn't show a thing, and of course the connection.query is also not running.

I'm not sure why this is happening.

Pretty sure this is a quite noobish node.js/callback question but I can't seem to find the proper code to make it run.

This is how I invoke my node-mysql code:

var utils = require('../../config/database/utils');

exports.getResults = function(callback) {   
  var query = "SELECT * FROM my_table"; 
  utils.exec(query, null, function(err, results){
    if(err){
      console.log(err);
      callback(true);
      return;
    }
    console.log(results);
    callback(false, results);
  });
};

Next is the utils file where I can't get the code work.

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

module.exports = {
getDBConnection: function() {
    pool.getConnection(function(err, connection){
        if(err){
            console.log(err);
            return;
        }
        return connection;
    });
},
endDBConnection: function(connection) {
    connection.end(function (err) {
        if(err) {
            console.log(err); 
            callback(true); 
            return; 
        }   
    });
},
exec: function(query, data, callback) {
    console.log(query);
    this.getDBConnection(function(err, connection){
        if(err){
            console.log('error');
        }
        console.log(connection);
        connection.query(query, data, function(err, results) {
            if(err) {
                callback(err);
            }
            callback(false, results);
        });
        this.endDBConnection(connection);
    });
}
}

Code is getting OK the the exec part since the console.log(query) logs the query. But after that, the code's not running, console.log(connection); doesn't show a thing, and of course the connection.query is also not running.

I'm not sure why this is happening.

Share Improve this question edited May 25, 2014 at 18:35 Alexander Kireyev 10.8k13 gold badges68 silver badges106 bronze badges asked May 25, 2014 at 18:33 Daniel Sh.Daniel Sh. 2,0746 gold badges42 silver badges68 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 2

Returning a value inside a callback is meaningless. You need to pass in a callback that gets called with the value you want to return:

getDBConnection: function(callback) {
    pool.getConnection(function(err, connection){
        if(err){
            console.log(err);
            return callback(err);
        }
        callback(null, connection);
    });
},

You should also use connection.release() instead of connection.end() since you are using a pool:

endDBConnection: function(connection) {
    connection.release();
},

In exec(), you have the wrong this. It should instead be something like:

exec: function(query, data, callback) {
    console.log(query);
    var self = this;
    this.getDBConnection(function(err, connection){
        if(err){
            console.log('error');
            return callback(err);
        }
        console.log(connection);
        connection.query(query, data, function(err, results) {
            self.endDBConnection(connection);
            if(err) {
                return callback(err);
            }
            callback(null, results);
        });
    });
}
发布评论

评论列表(0)

  1. 暂无评论