Federico Fioravanti
Federico Fioravanti

Reputation: 15

Questions about callbacks in node js using node-postgres and render page

I have postgresSql with a table of users(username, password e administrator(boolean)). Once I make my query (SELECT * FROM users WHERE username=$1 AND password=$2') I would like to check if administrator equal to true, then redirect to administrator page, if is false redirect to the normalClient page. I have a difficult understanding of callback. The first one can receive an error or the result, but if I receive result I need to check if administrator == true and then redirect to admin page else redirect to the client page

    router.post('/login',(req,res,next)=> {
      let text = 'SELECT * FROM users WHERE username=$1 AND password=$2';
      let values = [req.body.username,req.body.password];
     //callback
     //make query
      pool.query(text, values, (err, res) => {
      // if error print
      if (err) {
        console.log(err.stack)
        //if receive result print 
      } else {
        console.log(res.rows[0].administrator)
        // { name: 'brianc', email: '[email protected]' }
      }
    });

    res.redirect('/adminDashboard');
  });

Upvotes: 0

Views: 73

Answers (2)

Rash
Rash

Reputation: 8227

About your specefic issue, you need to move res.redirect('/adminDashboard'); inside your else block. Also reading previous comments I found that you are using the res variable in multiple places - router.post('/login',(req,res,next)=> { and pool.query(text, values, (err, res) => {. Call the value other than res in either place.

Since you mentioned you are having difficulties understanding callback, here is a little intro. Whenever you execute a function in Javascript, you need to check if that function is running synchronously or asynchronously. If async, like the function pool.query, you need to provide a callback - essentially another method that needs to be run when data is fetched from the original method.

When Javascript starts executing your code, it will call the pool.query method, but it will not wait for the results! Instead it will go and execute the res.redirect('/adminDashboard'); line of code. That is the reason why you need to move this line inside your callback.

And that is the beauty of Javascript. Having methods run async makes your overall program faster because now your thread is not sitting idle. It does not wait for the results but it goes to execute the next operation. It will come back and execute the callback function when your original method is done and has returned the data back.

Update

Per your comment, if you rename the res in your pool like this, pool.query(text, values, (err, queryRes) => {, you can still do res.redirect('/adminDashboard'); inside the else block because you still have access to the parent res variable.

Upvotes: 0

Guseyn Ismayylov
Guseyn Ismayylov

Reputation: 393

I think, you need to make redirect inside of callback in pool.query function.

Upvotes: 1

Related Questions