尝试宣传node-mysql连接并查询

时间:2016-01-20 16:35:17

标签: node.js promise bluebird

我试图在node-mysql包中使用bluebird的promisify。节点版本为4.2.4

var Promise = require('bluebird');
var mysqlClient = Promise.promisifyAll(require("mysql/lib/Connection").prototype);
Promise.promisifyAll(require("mysql/lib/Pool").prototype);

var connectionOptions = ({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'my_db'

});

var firstPost = "some sql here";

var results = [];
mysqlClient.connectAsync(connectionOptions).then(function(connection){
    connection.query(firstPost, function(){
        console.log('i reached line 26 of a node script. a minor miracle')

    });
}).catch(function(err) {
  console.log(err);
});

我收到错误

  

[TypeError:无法读取属性' socketPath'未定义的]

堆栈追踪:

Unhandled rejection TypeError: Cannot read property 'socketPath' of undefined
    at Connection.connect (/vagrant/spam_smasher/node_modules/mysql/lib/Connection.js:87:32)
    at Connection.tryCatcher (/vagrant/spam_smasher/node_modules/bluebird/js/release/util.js:11:23)
    at Connection.ret [as connectAsync] (eval at <anonymous> (/vagrant/spam_smasher/node_modules/bluebird/js/release/promisify.js:184:12), <anonymous>:13:39)
    at Object.<anonymous> (/vagrant/spam_smasher/bluebird.js:24:13)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Function.Module.runMain (module.js:467:10)
    at startup (node.js:136:18)
    at node.js:963:3

为什么我收到此错误和/或如何进一步调试?我尝试远程调试它,但是当我在原型链上走几步时,我不确定我在看什么。 Async函数似乎附加到MysqlClient对象。

Remore调试显示node-mysql的Connection.js文件中的以下部分引发了错误:

 if (!this._connectCalled) {
    this._connectCalled = true;

    // Connect either via a UNIX domain socket or a TCP socket.
    this._socket = (this.config.socketPath)
      ? Net.createConnection(this.config.socketPath)
      : Net.createConnection(this.config.port, this.config.host);
直接使用node-mysql时,

socketpath不是你需要定义的东西

1 个答案:

答案 0 :(得分:0)

似乎Bluebird文档已经过时了。他们想念你还需要ConnectionConfig,所以我的代码中缺少这个,导致错误。

我选择放弃这个问题来保持我的理智。还有进一步的讨论here,您可能还想查看mysql-promise