首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >节点- Postgres驱动程序安装与节点-postgres

节点- Postgres驱动程序安装与节点-postgres
EN

Stack Overflow用户
提问于 2017-10-09 12:10:00
回答 1查看 909关注 0票数 5

我正在尝试集成节点-postgres驱动程序,并学习做一个简单的CRUD操作。在我的app.js中,我做这样的事情:

代码语言:javascript
复制
...
var postgres = require('./adapters/postgres')
var postClient = new postgres(conf);
...
postClient.connect(function (dbconn) {
    app.dbconn = dbconn;
    app.conf = conf;

    console.log("************************************************************");
    console.log(new Date() + ' | CRUD Server Listening on ' + conf['web']['port']);
    console.log("************************************************************");

    server.listen(conf['web']['port']);

    var Routes = require('./routes/http-routes');
    new Routes(app);
});

在我的adapters/postgres.js文件中,我有以下内容:

代码语言:javascript
复制
const Client = require('pg');
const postClient = new Client(conf)({
    host: conf['postgres'].host,
    port: conf['postgres'].port,
    dbname: conf['postgres'].dbname,
    username: conf['postgres'].username,
    password: conf['postgres'].password,
    dbconn: null,
});
module.exports = postClient;
postClient.prototype.connect = function (cbk) {
    var self = this;
    client.connect(function (err, db) {
        console.log(new Date() + " | Postgres Server Connection Establised...");
        console.log(new Date() + " | Current database: ", db.databaseName);
        if (!err) {
            console.log(new Date() + " | Postgres Server Authenticated...");
            self.dbconn = db;
            cbk(db);
        } else {
            console.log(new Date() + " | Postgres Server Error in connection...");
            console.log(err);
            self.dbconn = db;
            cbk(db);
        }
    });
};

使用上面的代码,我一直得到这个错误:ReferenceError: conf is not defined,所以我将它添加为var conf = require('../config/conf');。这不是一个合适的解决方案,因为我想从app.js中传递它。接下来,即使添加了这些内容,我仍然会得到以下错误:TypeError: Client is not a constructor。有人能指导修复这两种错误吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-13 09:53:09

导出接受配置对象并返回客户端实例的工厂函数。Client不是pg中的默认导出,因此需要取消结构。

代码语言:javascript
复制
const { Client } = require('pg');

const createPgClient = (conf) => {
    const pgClient = new Client(conf)({
        host: conf['postgres'].host,
        port: conf['postgres'].port,
        dbname: conf['postgres'].dbname,
        username: conf['postgres'].username,
        password: conf['postgres'].password,
        dbconn: null,
    });

    pgClient.prototype.connect = function (callback) {

        client.connect()
        .then(() => {
            console.log(new Date() + " | Postgres Server Authenticated...");
        })
        .catch((err) => {
            console.log(new Date() + " | Postgres Server Error in connection...");
            console.log(err);
        })
        .finally(()=> {
            self.dbconn = this;
            callback(this);
        });

    };
    return pgClient;
};
module.exports = createPgClient;

app.js中使用这个工厂函数来创建一个客户端。

代码语言:javascript
复制
const createPgClient = require('./adapters/postgres')
cont pgClient = createPgClient(conf);

pg.connect(function (dbconn) {
    app.dbconn = dbconn;
    app.conf = conf;

    console.log("************************************************************");
    console.log(new Date() + ' | CRUD Server Listening on ' + conf['web']['port']);
    console.log("************************************************************");

    server.listen(conf['web']['port']);

    const Routes = require('./routes/http-routes');
    new Routes(app);
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46646127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档