2016-01-20 21 views
0

我試圖使用bluebird的promisify與node-mysql包。節點版本是4.2.4未處理的拒絕TypeError試圖promisify節點mysql連接和查詢

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: Cannot read property 'socketPath' of undefined]

堆棧跟蹤:

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對象。

遙遠而明亮的調試顯示,在節點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); 

socketpath是不是你需要使用節點MySQL時定義的東西直接

+0

向我們展示周圍的代碼(什麼是'this.config')? –

+0

@BenjaminGruenbaum。感謝您的回覆。配置代碼在這裏,如果你有興趣https://github.com/felixge/node-mysql/blob/master/lib/ConnectionConfig.js,但我放棄了這個問題,根據我的答案 – codecowboy

回答

0

它似乎藍鳥文檔已過時。他們錯過了你也需要ConnectionConfig可用,所以這在我的代碼中缺失,導致錯誤。

我選擇放棄這個問題來保持我的理智。有進一步的討論here,你也可能想結賬mysql-promise

相關問題