2

I'm trying to connect a Node.js app with a PostgreSQL server. It seems that no matter what I use, I end up with the same error:

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

At first I was declaring a new pg.Client() like the example in the documentation here, but got the above error discovered that might be a bad idea according to this stack overflow post.

I tried using pg.connect():

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

console.log("Initiating...");
//var connectionString = "postgres://postgres:[email protected]/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;
      }
    });
  });
});

Here is the pg-promise code that I tried:

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);
    });

I must be missing something, but I don't know where to look. Thank you to anyone who can help me figure out what this error means.

3
  • Your pg-promise usage is incorrect. Instead of var pgp = require('pg-promise'); there must be var pgp = require('pg-promise')(/*initialization options*/);, as per the documentation. Commented Feb 2, 2017 at 8:43
  • @vitaly-t, thank you for responding. I'm using Browserify to define require() in my application, and when I add the second set of parameters - var pgp = require('pg-promise')(); as in the documentation, I get require(...) is not a function. Should I be using something besides Browserify? Commented Feb 2, 2017 at 18:47
  • pg-promise is strictly a server-side module, why on earth would you ever need to use Browserify on it? :) Commented Feb 2, 2017 at 20:05

1 Answer 1

0

Make sure you are not crossing a context boundary that is corrupting the net prototype chain and stripping away methods like Stream(). I ran into a similar unhandled Promise exception w Node 7.5 and pg-live-select. However it was intermittent because of the way the net reference was being passed around. I ended up using V8 inspector and putting a 'debugger' statement directly above line 13 in connection.js to catch the corruption.

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.