尝试承诺 node-mysql 连接和查询时出现未处理的拒绝 TypeError
Unhandled rejection TypeError when trying to promisify a node-mysql connection and query
我正在尝试将 bluebird 的 promisify 与 node-mysql 包一起使用。节点版本为 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: 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
为什么我会收到此错误and/or 我该如何进一步调试它?我尝试远程调试它,但不确定当我沿着原型链向下几步时我在看什么。 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
不是直接使用node-mysql需要定义的东西
Bluebird 文档似乎已过时。他们想念您还需要 ConnectionConfig 可用,因此我的代码中缺少它,导致错误。
为了保持理智,我选择放弃这个问题。还有进一步讨论here and you may also want to check out mysql-promise
我正在尝试将 bluebird 的 promisify 与 node-mysql 包一起使用。节点版本为 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: 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
为什么我会收到此错误and/or 我该如何进一步调试它?我尝试远程调试它,但不确定当我沿着原型链向下几步时我在看什么。 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
不是直接使用node-mysql需要定义的东西
Bluebird 文档似乎已过时。他们想念您还需要 ConnectionConfig 可用,因此我的代码中缺少它,导致错误。
为了保持理智,我选择放弃这个问题。还有进一步讨论here and you may also want to check out mysql-promise