Question:
SelectDb.js Database File
module.exports = new promise(function(resolve,reject){
pool.connect(function(err,client,done){
if(err) console.log(err.toString());
else {
client.query('SELECT text FROM public.message;', function (err,
result) {
done();
console.log("I make new Request");
if(result) {
resolve(result);
//client.end();
}
})
}
});
})
On the first request, I get data from the database; on the second request, it does not work. When adding data in the database, they are displayed in the database itself, and a repeated request does not send updated data, but the original. I don't understand, I have to close the connection myself and re-request to get the updated data? done()
placed in the place I wanted, according to the documentation. Trying to close it gives me an error:
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
As I understand it, I put the closure in the wrong place or nafig you close it if I haven't returned the data yet. I'm not sure if I understand at all what actions are required for adequate work.
Answer:
Bottom line, just call the function. And all the problems have been resolved. I do not know how much this is correct.
module.exports.SelMsg = function MsgSel(){
return new Promise(function(resolve,reject){
pool.connect()
.then(function (client) {
client.query('SELECT text FROM public.message;')
.then(function (res) {
resolve(res);
client.release();
}).catch(function (e) {
console.log(e);
})
})
})
}