Answer the question
In order to leave comments, you need to log in
Why doesn't pool work in PostgreSQL?
Hey! I follow the instructions from the Internet, but it does not work. An error occurs after the select:
Error: Connection terminated
at Connection.con.once (/Users/olegostashov/Documents/Study/PMIS/node_modules/pg/lib/client.js:170:29)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)
at Connection.emit (events.js:208:7)
at Socket.<anonymous> (/Users/olegostashov/Documents/Study/PMIS/node_modules/pg/lib/connection.js:122:10)
at emitNone (events.js:111:20)
at Socket.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
const express = require("express");
const bodyParser = require("body-parser");
const config = require("./config");
const pg = require("pg");
const pg_connect = {
user: config.user,
host: config.host,
database: config.database,
password: config.password,
port: config.port_pg
};
const app = express();
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", (req, res) => {
var pool = new pg.Pool(pg_connect);
const results = [];
pool.connect(function(err, client, done) {
if (err) {
done();
console.log(err);
return res.status(500).json({ success: false, data: err });
} else {
client.query("SELECT * FROM users", (err, res) => {
console.log(err);
});
done();
}
console.log(results);
res.render("index", { users: results });
});
pool.end();
});
module.exports = app;
Answer the question
In order to leave comments, you need to log in
Возможно, кому-то понадобится. Удалил лишний pool.end() и перенес res.render().
app.get("/", (req, res) => {
var pool = new pg.Pool(pg_connect);
const results = [];
pool.connect(function(err, client, done) {
if (err) {
console.log(err);
done();
return res.status(500).json({ success: false, data: err });
} else {
client
.query("SELECT * FROM users")
.then(result => {
result.rows.forEach(function(item) {
results.push(item);
});
res.render("index", { users: results });
})
.catch(err => console.log(err));
done();
}
});
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question