The weird thing is that console.log(response) inside the callback of the query function gives me the expected output of the variable response but when inspecting it outside the callback of the query function it returns the initial value of the response which is an empty object!? So the assignment inside the callback is not updating the global variable why! Is there a way around this please?.
const response = {
error: "",
data: {}
};
pool.query(`INSERT INTO client(username, userEmail, userLocation, userOccupation, userPassword)
VALUES($1, $2, $3, $4, $5) RETURNING *`, user,
(err, result) => {
if (err) {
if (err.constraint === "client_useremail_key")
response.error = "This email has been used, please choose another one.";
else response.error = err;
return;
};
response.data = result.rows;
// console.log(response.data.rows);
});
console.log(response);