node.js - nodejs、postgres 和 Bluebird

标签 node.js postgresql bluebird

我一直在尝试将 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/

相关文章:

javascript - JS Promise 等待刷新 token

javascript - Bluebird .js : Re-Throwing the error in catch callback is not caught again

node.js - 使用异步作业在 node.js/express 中正常关闭

node.js - sinon stub 在 Node.js 测试中不起作用

javascript - 如何做滑动缓冲流?

postgresql - 调整慢速分组函数 Invantive SQL

javascript - 是否可以将生成器中的项目异步收集到数组中?

node.js - Firebase云功能-Reference.update失败

sql - 如何在维护小结果集的同时从 SQL 填充对象的列表属性?

sql - PostgreSQL:如何将数组传递给函数并在带有 IN 运算符的查询中使用它