Node – 使用node-postgres设置Postgres驱动程序
我正在尝试整合node-postgres驱动程序,并学习做一个简单的CRUD操作。 在我的app.js
,我做了这样的事情:
... 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
文件中,我有以下内容:
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');
。 这不是一个适当的解决scheme,因为我想从app.js
传递它。 接下来,即使这添加我得到以下错误: TypeError: Client is not a constructor
。 有人可以指导解决这两个错误?
导出一个带有configuration对象并返回客户端实例的工厂函数。 Client
不是pg
的默认导出,因此需要进行解构。
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
使用这个工厂函数来创build一个客户端。
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); });
- webpack与node-postgres导入错误('pg'.Client)
- node-postgres与callback或asynchronous/等待交易?
- Node-Postgres查询方法不调用它的callback函数
- node-postgres:caching的参数化插入查询,在第二次运行时失败
- 将嵌套的JSON从多个到多个从PostgreSQL连接到Node.js的表中返回
- 用brianc / node-postgres批量插入到Postgres中
- 带有json数组的Postgresql中的参数化查询
- 节点服务器无法连接到postgres数据库
- 如何将MySQL风格的问号`?`绑定参数转换为Postgres风格的`$ 1`绑定参数