2017-10-10 4 views
0

node.jsとpostgresをdbにして残りのapiのコードを書いています。 私のシステムでは郵便配達員でうまく動作していますが、herokuサイトに「アプリケーションエラー」が表示されていることを私はherokuに同じコードを配備しています。私はNode.jsのherokuのサイトでpostgres dbを使って "Application Error"を取得する

My code : 


const pg = require('pg'); 
var Sequelize=require ('sequelize'); 
var app = require('express')();   // Express App include 
var http = require('http').Server(app); // http server 
var env = app.get('env') == 'development' ? 'dev' : app.get('env'); 
var port = process.env.PORT || 8086; 
var bodyParser = require("body-parser"); // Body parser for fetch posted data 

app.use(bodyParser.urlencoded({ extended: false })); 
app.use(bodyParser.json()); // Body parser use JSON data 

var sequelize = new Sequelize('testdb', 'postgres', '[email protected]', { 
    host: 'localhost', 
    port: 5432, 
    dialect: 'postgres' 
}); 
sequelize 
    .authenticate() 
    .then(() => { 
    console.log('Connection has been established successfully.'); 
    }) 
    .catch(err => { 
    console.error('Unable to connect to the database:', err); 
    }); 

    app.get('/Getdata',function(req,res){ 
    var data = { 
     "Data":"" 
    }; 



    //GET method 
    sequelize.query("SELECT * FROM test_table ", { replacements: { visible: 'true' },type: sequelize.QueryTypes.SELECT}) 
    .then(function(test_table,err,rows) { 
    // We don't need spread here, since only the results will be returned for select queries 
    if(test_table) 
    { 
      data["Data"] = test_table; 
      res.json(data); 
     } 
     else 
     { 
      data["Data"] = 'No data Found..'; 
      res.json(data); 
     } 

    }); 
}); 


When I run "heroku logs --tail --app boiling-river-40656 
" in terminal that shows following error. 


**Unable to connect to the database: { SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:5432** 
2017-10-10T07:55:19.636702+00:00 app[web.1]:  at Connection.connectingErrorHandler (/app/node_modules/pg/lib/client.js:123:14) 
2017-10-10T07:55:19.636703+00:00 app[web.1]:  at emitOne (events.js:96:13) 
2017-10-10T07:55:19.636701+00:00 app[web.1]:  at connection.connect.err (/app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:96:24) 
2017-10-10T07:55:19.636703+00:00 app[web.1]:  at Connection.emit (events.js:188:7) 
2017-10-10T07:55:19.636705+00:00 app[web.1]:  at emitOne (events.js:96:13) 

でデシベルを接続するsequelizeを持っている私は、問題を解決するのに役立ちます。

ありがとうございました。

答えて

0

ログには、Herokuの127.0.0.1:5432に接続できないというメッセージがあります。 127.0.0.1:5432にはpostgresサーバーはありません。 heroku postgresアドオン( "Hobby dev"という無料のプランがあります)を使用してください。

厳密に環境変数を宣言することによってHerokuの ENVからDEV ENVを分離し、あなたのコードでそれを消費することをお勧めします。 データベースのURL、pwd、ユーザー名はenvまたは設定ファイルに移動する必要があります。

+0

同じ問題:https://stackoverflow.com/a/40993195/6753239 – ninjarails

関連する問題