Search code examples
node.jspostgresqlexpresscallbackpg

Nodejs and postgres: returning data from query for password validation


I'm new to javascript/nodejs and I'm trying to build a simple login where I have a function which is supposed to return the query result.

How I configured the connection;

pg.defaults.ssl = true;
let dbClient = new pg.Client(conString);
dbClient.connect();

The callback function;

    app.post("/login", urlencodedParser, function(req, res) {

  let uName = req.body.username;
  let pwdInput = req.body.password;
  let fetchedPwd;

  function fetchPwd (usr, callback) {

     dbClient.query("SELECT pwd FROM users where username = $1", [usr], function (err, res) {

       if (err) {
         callback(err, null);
        }

        else {
          callback (null, res[0].pwd);
        }
     });}

function call;

fetchPwd(uName, function(err, res) {
if (err) {
  console.log(err);
}
else {
  fetchedPwd = res;
}})

the check;

  if (pwdInput == fetchedPwd) {
    req.session.user = uName;
    res.redirect("/");
  }

  else {

    res.render("login", {login_error: "Wrong user and password combination!"});
  }

});

What I expected; The callback function to return a String which I then could check against the input.

Encountered errors; Error: Client was closed and is not queryable

for testing purposes I commented out the dbClient.end(); at the end of the file after that the line callback (null, res[0].pwd); throws the following error; TypeError: Cannot read properties of undefined (reading 'pwd')

(pwd is the correct column name in the DB)

I don't understand why I'm getting either of these errors. From what I expected, the function should've finished before dbClient.end(); is reached.

Also for the second error; How can res[0].pwd be undefined, when the query result apparently isn't empty, since the else statement is reached?


Solution

  • I've found an answer as to why I encountered the mentioned errors;

    1. The weird behaviour around the DB connection was because of the configuration of pg being outdated. I've now changed the configuration to:

      const dbConfig = {
             connectionString: conString,
             ssl: { rejectUnauthorized: false }
           }
           var dbClient = new pg.Client(dbConfig);
           dbClient.connect();
      

    also I'm not closing the connection anymore but just keeping it alive.

    1. while I'm still not sure why my old function didn't work, I've rewritten my function and now everything works as intended;

      app.post("/login", urlencodedParser, function(req, res) {
      
      let uName = req.body.username;
      let pwdInput = req.body.password;
      
      let User = {
       username: uName,
       password: ""
      }
      
      function fetchPwd (usr) {
       dbClient.query("SELECT pwd from users where username = $1", [usr], function (dbError, dbResponse) {
      
         User.password = dbResponse.rows[0].pwd;
      
         if (pwdInput == User.password) {
           req.session.user = uName;
           res.redirect("/");
         }
      
         else {
           res.status(400).render("login", {login_error: "Wrong user and password combination!"});
           console.log("wrong login");
         }
       });
      }
      
      fetchPwd(User.username);
      
      });