如何在nodejs中连接heroku postgres数据库

时间:2018-03-18 13:12:21

标签: node.js postgresql

我在heroku服务器上创建了一个应用程序并安装了Postgres免费附加组件。现在我有一个nodejs项目,我使用 pg 模块连接这个数据库。所以为此,我创造了

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

然后在API层中,我导入了这个文件

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

并像这样使用

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

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

这显示以下错误

(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

然后我在配置对象

中启用了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
};

但现在显示

(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)

问题是什么?我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

实际上,我错过了传递client.query()中的查询。它应该是client.query(query)。这是代码

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

另一种是使用pool.query

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

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