【问题标题】:net.Stream is not a constructor - Node Postgresnet.Stream 不是构造函数 - 节点 Postgres
【发布时间】:2017-06-19 05:59:03
【问题描述】:

我正在尝试将 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);
    });

我一定错过了什么,但我不知道该去哪里找。感谢任何可以帮助我弄清楚这个错误意味着什么的人。

【问题讨论】:

  • 您的pg-promise 用法不正确。根据文档,必须使用 var pgp = require('pg-promise')(/*initialization options*/); 而不是 var pgp = require('pg-promise');
  • @vitaly-t,感谢您的回复。我正在使用 Browserify 在我的应用程序中定义 require(),当我添加第二组参数 - var pgp = require('pg-promise')();,如文档中所示,我得到 require(...) is not a function。我应该使用 Browserify 以外的东西吗?
  • pg-promise 严格来说是一个服务器端模块,你到底为什么需要在它上面使用 Browserify? :)

标签: node.js postgresql node-postgres


【解决方案1】:

确保您没有越过会破坏网络原型链并剥离 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-21
    • 2021-09-28
    • 1970-01-01
    • 2020-11-25
    • 2020-11-24
    • 1970-01-01
    • 2016-02-15
    • 2023-03-04
    相关资源
    最近更新 更多