I am trying to integrate a node-postgres driver and learn how to do simple CRUD operations. In mine, app.js
I am doing something like this:
...
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);
});
Inside my file adapters/postgres.js
, I have the following content:
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);
}
});
};
With the code above, I keep getting this error: ReferenceError: conf is not defined
so I added it as var conf = require('../config/conf');
. This is not the right solution, as I would like to pass it from app.js
. Further, even with this addition, I get the following error: TypeError: Client is not a constructor
. Can anyone be guided by fixing both of these errors?
source
share