Every time I try to access the data stored in the variable I get an empty array. Is there a way around this? I have tried using pg.Pool but it didn’t work. I was expecting the numberOfUsers variable to contain the result from the query. This is the code
enter image description here
const userDb = new pg.Client({
user: "postgres",
host: "localhost",
database: "Project DB",
password: "1567tay",
port: 5432,
});
userDb.connect();
function userNameCount(userName){
let numberOfUsers = [];
userDb.query("SELECT COUNT(*) FROM Users WHERE username = $1", [userName], (err, res) => {
if (err) {
console.error("Error executing query", err.stack);
} else {
numberOfUsers = res.rows;
}
userDb.end();
});
return numberOfUsers;
}
New contributor
nyameget is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.