简体   繁体   English

如何在Node.js中连接Heroku Postgres数据库

[英]how to connect heroku postgres database in nodejs

I have created an app on heroku server and installed Postgres free add-on. 我已经在heroku服务器上创建了一个应用程序,并安装了Postgres免费插件。 Now I have a nodejs project in which I am connecting this database using pg modules. 现在,我有一个nodejs项目,其中使用pg模块连接此数据库。 So for this I have created 为此,我创建了

db-connect.js db-connect.js

var { Pool } = require('pg'); 
var nodeEnvFile = require("node-env-file");
nodeEnvFile(".env");

var config = {
    user: process.env.DB_USER,
    host: process.env.DB_IP,
    database: process.env.DB,
    password: process.env.DB_PASSWORD,
    port: process.env.DB_PORT,
    max: 10, // max number of connection can be open to database
    idleTimeoutMillis: 30000, // how long a client is allowed to remain idle before being closed
};

var pool = new Pool(config);

module.exports = {
    query: (query, callback) => {
        console.log(query);
        pool.connect().then(client => {
            return client.query()
                .then((result) => {
                    client.release();
                    console.log(result.rows)
                    callback(null, result.rows[0]);
                })
                .catch(err => {
                    client.release();
                     callback(err, null);
                });
        })
    }
}

and then in the API layer, I have imported this file 然后在API层中,我已经导入了此文件

const db = require("../db/db-connect");

and used like this 像这样使用

router.get("/getdata/", (req, res) => {
        var query = "query";

        db.query(query, (err, result) => {
            if (err) {
                res.status(400).send(err);
            }
            res.send(result);
        })
    });

and this was showing following error 这显示了以下错误

(node:1984) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): error: no pg_hba.conf entry for host "157.39.161.5", user "ltolmhjmwnfokl", database "den55ln368anf8", SSL off
(node:1984) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
select * from get_notifications('sidhu',0,1);
(node:1984) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): error: no pg_hba.conf entry for host "157.39.161.5", user "ltolmhjmwnfokl", database "den55ln368anf8", SSL off

and then I enabled ssl option in the config object 然后我在配置对象中启用了ssl选项

var config = {
    user: process.env.DB_USER,
    host: process.env.DB_IP,
    database: process.env.DB,
    password: process.env.DB_PASSWORD,
    port: process.env.DB_PORT,
    max: 10, // max number of connection can be open to database
    idleTimeoutMillis: 30000, // how long a client is allowed to remain idle before being closed
    ssl: true
};

but now this is showing 但是现在这表明

(node:252) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'submit' of undefined
(node:252) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
ServiceUnavailableError: Response timeout
    at IncomingMessage.<anonymous> (D:\PROJECTS\PuWifi\GitHubForHeroKu\PuWifi\node_modules\connect-timeout\index.js:84:8)
    at emitOne (events.js:116:13)
    at IncomingMessage.emit (events.js:211:7)
    at Timeout._onTimeout (D:\PROJECTS\PuWifi\GitHubForHeroKu\PuWifi\node_modules\connect-timeout\index.js:49:11)
    at ontimeout (timers.js:475:11)
    at tryOnTimeout (timers.js:310:5)
    at Timer.listOnTimeout (timers.js:270:5)

what is the issue? 有什么问题? Am I missing something? 我想念什么吗?

Actually, I was missing to pass the query in client.query() . 实际上,我缺少在client.query()传递查询 It should be client.query(query) . 它应该是client.query(query) Here is the code 这是代码

module.exports = {
    query: (query, callback) => {
        console.log(query);
        pool.connect().then(client => {
            return client.query()
                .then((result) => {
                    client.release();
                    console.log(result.rows)
                    callback(null, result.rows[0]);
                })
                .catch(err => {
                    client.release();
                     callback(err, null);
                });
        })
    }
}

The other is to use pool.query 另一种是使用pool.query

module.exports = {
    query: (query, callback) => {
        console.log(query);
        pool.query(query).then(response => {
            callback(null, response.rows);
        }).catch(err => {
            callback(err, null);
        })
    }
}

For detail: https://github.com/brianc/node-postgres/issues/1597#issuecomment-375554709 有关详细信息: https : //github.com/brianc/node-postgres/issues/1597#issuecomment-375554709

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM