我一直在尝试将 bluebird promises 与 pg 库一起使用,甚至找到了这篇文章,但遗憾的是,我是 StackOverflow 用户的新手,无法直接在此处发表评论:Manually promisifying pg.connect with Bluebird
简而言之,我在剪切和粘贴该代码后使用 bluebird Promisfy 函数尝试的所有操作都不会引入任何查询构造函数,并且我可能在我所做的尝试中滥用了 ClientAsync 函数,但我希望这是一个快速而简单的帮助,因为我尝试的一切都会导致以下变化:
Possibly unhandled TypeError: Object function (err) {
if(err) {
pool.destroy(client);
} else {
pool.release(client);
}
} has no method 'queryAsync'
我转储了 PromisfyAll 函数结果,并且确实不存在 queryAsync:
相关片段:
Client: { [Function] Query: { [Function] super_: [Object] } },
Query:
{ [Function]
super_: { [Function: EventEmitter] listenerCount: [Function] } },
pools:
{ all: {},
Client: { [Function] Query: [Object] },
getOrCreate: [Function] },
Connection:
{ [Function]
super_: { [Function: EventEmitter] listenerCount: [Function] } },
types:
{ getTypeParser: [Function],
setTypeParser: [Function],
arrayParser: { create: [Function] } },
ClientAsync: { [Function: ClientAsync] __isPromisified__: true },
endAsync: { [Function: endAsync] __isPromisified__: true },
connectAsync: { [Function: connectAsync] __isPromisified__: true },
cancelAsync: { [Function: cancelAsync] __isPromisified__: true },
setMaxListenersAsync: { [Function: setMaxListenersAsync] __isPromisified__: true },
emitAsync: { [Function: emitAsync] __isPromisified__: true },
addListenerAsync: { [Function: addListenerAsync] __isPromisified__: true },
onAsync: { [Function: onAsync] __isPromisified__: true },
onceAsync: { [Function: onceAsync] __isPromisified__: true },
removeListenerAsync: { [Function: removeListenerAsync] __isPromisified__: true },
removeAllListenersAsync: { [Function: removeAllListenersAsync] __isPromisified__: true },
listenersAsync: { [Function: listenersAsync] __isPromisified__: true } }
它在解析中找到了相关函数,但没有 promise 查询:有谁知道我如何进一步解决这个问题或使用 ClientAsync 执行 SQL 查询的潜在语法?我试图从 Bluebird github 页面上的信息手动添加 pg query.js 文件,但无济于事。
最佳答案
事实证明,使用 Promisfy 时,Javascript 与 native 绑定(bind)库存在差异。
var pg = require('pg');
var Promise = require('bluebird');
var db = Promise.promisifyAll(pg);
var connectionString = "postgres://node:node@localhost:5432/postgres";
db.connectAsync("postgres://node:node@localhost:5432/postgres").spread(function(connection, release) {
return connection.queryAsync("select * from howdy")
.then(function(result) {
console.log("rows", result.rows);
})
.finally(function() {
release();
});
});
有效,而这个:
var pg = require('pg').native;
var Promise = require('bluebird');
中断丑陋的错误消息。
我想我最终需要在各种选项(bluebird w/promisfy 和 JS 绑定(bind)与 C 绑定(bind) (libpq) 和手动 promise )之间做一些基准测试。
关于node.js - nodejs、postgres 和 Bluebird ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24484644/