node.js - net.Stream 不是构造函数 - Node Postgres

标签 node.js postgresql node-postgres

我正在尝试将 Node.js 应用程序与 PostgreSQL 服务器连接起来。似乎无论我使用什么,我最终都会遇到同样的错误:

bundle.js:16177 ERROR: TypeError: net.Stream is not a constructor
at new Connection (bundle.js:10133)
at new Client (bundle.js:9704)
at Object.create (bundle.js:11308)
at Pool._createResource (bundle.js:510)
at Pool.dispense [as _dispense] (bundle.js:498)
at Pool.acquire (bundle.js:573)
at Pool.pool.connect (bundle.js:11359)
at PG.connect (bundle.js:10876)
at bundle.js:1642

起初我声明了一个 new pg.Client() 就像文档中的例子 here , 但根据 this 发现上述错误可能不是一个好主意堆栈溢出帖子。

我尝试使用 pg.connect():

var pg = require('pg'); //postgresql dependency
var connectionString = "postgres://postgres:thisissuchagoodpassword@PostgreSQL/localhost:5432/Milestone1DB"

console.log("Initiating...");
//var connectionString = "postgres://postgres:thisissuchagoodpassword@PostgreSQL9.6/localhost:5432/Milestone1DB";
//var client = new pg.Client();

//connect to the database
console.log("Attempting to connect to the database");
pg.connect(function (err, client, done)
{
  if(err)
  {
    console.log("Error connecting to the database.");
     throw err;
  }

  client.query("SELECT DISTINCT state FROM business ORDER BY state", function (err, result)
  {
   if(err)
    {
      console.log("Query resulted in an error.");
      throw err;
    }

    console.log(result.rows[0]);

    client.end(function (err)
    {
      if(err)
      {
        console.log("Error disconnecting from the databse.");
        throw err;
      }
    });
  });
});

这是我试过的 pg-promise 代码:

var pgp = require('pg-promise');

var cn = {
    host: 'localhost', // server name or IP address;
    port: 5432,
    database: 'Milestone1DB',
    user: 'postgres',
    password: 'thisissuchagoodpassword'
};

var db = pgp(cn); // database instance;

db.any("select distict state from business order by state;")
    .then(data => {
        console.log("DATA:", data);
    })
    .catch(error => {
        console.log("ERROR:", error);
    });

我一定是遗漏了什么,但我不知道去哪里找。感谢任何能帮我弄清楚这个错误是什么意思的人。

最佳答案

确保您没有跨越破坏网络原型(prototype)链并剥离 Stream() 等方法的上下文边界。我在 Node 7.5 和 pg-live-select 中遇到了类似的未处理的 Promise 异常。然而,由于网络引用的传递方式,它是断断续续的。我最终使用了 V8 检查器并在 connection.js 的第 13 行正上方放置了一个“调试器”语句来捕获损坏。

node_modules/lib/connection.js:13
  this.stream = config.stream || new net.Stream();
                                 ^

TypeError: net.Stream is not a constructor
    at new Connection (node_modules/pg-live-select/node_modules/pg/lib/connection.js:13:34)
    at new Client (node_modules/pg-live-select/node_modules/pg/lib/client.js:26:37)
    at Object.create (node_modules/pg-live-select/node_modules/pg/lib/pool.js:27:24)
    at Pool._createResource (node_modules/generic-pool/lib/generic-pool.js:325:17)
    at Pool.dispense [as _dispense] (node_modules/generic-pool/lib/generic-pool.js:313:12)
    at Pool.acquire (node_modules/generic-pool/lib/generic-pool.js:388:8)
    at Pool.pool.connect (node_modules/pg-live-select/node_modules/pg/lib/pool.js:78:14)
    at PG.connect (node_modules/pg-live-select/node_modules/pg/lib/index.js:49:8)
    at LivePg._updateQuery (node_modules/pg-live-select/index.js:295:6)
    at node_modules/pg-live-select/index.js:160:14
    at Array.forEach (native)
    at Timeout.performNextUpdate [as _onTimeout] (node_modules/pg-live-select/index.js:159:23)
    at ontimeout (timers.js:365:14)
    at tryOnTimeout (timers.js:237:5)
    at Timer.listOnTimeout (timers.js:207:5)

关于node.js - net.Stream 不是构造函数 - Node Postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41997556/

相关文章:

node.js - 使用 AWS EB Worker 时的自定义 header

angularjs - Stormpath 不允许我在登录时访问路线

node.js - 在 Angular 和 Node.js 项目之间共享代码

javascript - mocha 错误 : No test files found: "test/" npm ERR! 测试失败

postgresql - 由于其他两列的差异而产生的列

node.js - 使用 Sequelizejs 检索多行

python - Django 在bulk_create 中设置自动字段值

postgresql - docker-compose:postgres 数据不持久

node.js - 将 Async/Await 与 node-postgres 一起使用

node.js - 通过模块导出重用 pg-pool