2017-02-02 14 views
1

Node.jsアプリケーションをPostgreSQLサーバに接続しようとしています。私が使用しているものに関係なく、私は同じエラーで終わるようだ:net.Streamはコンストラクタではありません - Node Postgres

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 

最初に私はドキュメントhereの例のようにnew pg.Client()を宣言しましたが、上記のエラーがそれは悪いかもしれない発見してしまいましたthisスタックオーバーフローポストによると、

私はpg.connect()を使用してみました:私は何かが欠けする必要があります

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

を、私はどこを見れするかわからない:ここでは

var pg = require('pg'); //postgresql dependency 
var connectionString = "postgres://postgres:[email protected]/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; 
     } 
    }); 
    }); 
}); 

は、私が試したPG-約束コードです。このエラーが何を意味するのか理解できる人に感謝します。

+0

あなたの 'pg-promise'の使い方が間違っています。 'var pgp = require( 'pg-promise');の代わりに' 'var pgp = require(' 'pg-promise ')(/ *初期化オプション* /);'を書かなければなりません。 –

+0

@ vitaly-t、ありがとうございます。私は、アプリケーションで 'require()'を定義するためにBrowserifyを使用しています。ドキュメントの 'var pgp = require( 'pg-promise')();'という2番目のパラメータを追加すると、 require(...)は関数ではありません。私はBrowserifyの他に何かを使っているべきですか? –

+0

'pg-promise'は厳密にはサーバー側モジュールです。なぜなら、Cloudifyを使用する必要があるのはなぜですか? :) –

答えて

0

ネットプロトタイプチェーンを破損し、Stream()のようなメソッドを取り除くコンテキスト境界を超えていないことを確認してください。私はNode 7.5とpg-live-selectと同様の未処理のPromise例外が発生しました。しかし、ネット参照が回っていたために断続的でした。私はV8インスペクタを使用し、connection.jsの13行目の直上に 'debugger'ステートメントを置いて破損を捕まえました。

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) 
関連する問題