I'm having trouble creating a module in nodejs
to export my connection to PostgreSQL
.
I could not understand why the second code does not work because I would like to export connect
already.
Code 1 .
var pg = require('pg');
module.exports = function () {
var conString = 'postgres://usuario:senha@localhost:5432/teste';
var conexao = new pg.Client(conString);
return conexao;
};
I call this in my app:
var db = require('../../config/conexao');
var conexao = db();
conexao.connect();
conexao.query('select * from cliente', function (error, result) {..
Code 2
var pg = require('pg');
module.exports = function () {
var conString = 'postgres://usuario:senha@localhost:5432/teste';
var conexao = new pg.Client(conString);
// retorna já a conexao feita
return conexao.connect();
};
And I call it that:
var conexao = db();
Without using
connect
, so I get the error that thequery
method does not exists.
Because of the second way, I can not find the query
method in my connection?