0

I'm writing a function userLogin which returns signedInDetails(in object form), when called in /userSignUp route's query. I tried to add async awaits, promises and even setTimeOut . But all these options are still giving abc as undefined. what's that I'm writing wrong?

Edit: I have looked into other questions. I'm not able to still solve it.


const express = require('express');
const app = express();

*
*
*
*

function userLogin(cookies){
    let serverCookies;
    let signInStatus;
    let userId;
    let signedInDetails;

    const HashQuery = {
      text: 'select * from users where session_id = $1',
      values: [cookies]
    }

    client.query(HashQuery, (err, res) => {
      if(res.rows.length === 0){
        return false;
      }else{ 
          res.rows.map((i) => {
             serverCookies = i.session_id;
             userId = i.user_id;
          });

          if(parseInt(cookies) === parseInt(serverCookies)){
              signInStatus = true;
              signedInDetails = {
                 userid: userId,
                 signinstatus: signInStatus
              }
              return signedInDetails
          } 
      }
      client.end();
    })
}




app.get('/', (req, res) => {
  if(req.cookies.hash_an_app === undefined){
    res.cookie('hash_an_app', hash);
  }else{
    userLogin(req.cookies.hash_an_app)
  }
  res.render('index');
});




app.post('/userSignUp', (req, res, next) => {
  
    const checkQuery = {
      text: 'select * from users where user_name = $1',
      values: [req.body.uName]
    }


    const insertQuery = {
       text: 'INSERT INTO users(f_name, l_name, user_name, password, session_id) VALUES($1, $2, $3, $4, $5)',
       values: [req.body.fName, req.body.lName, req.body.uName, req.body.pw, req.cookies.hash_an_app]
  }


    client.query(checkQuery, (err, res) => {
       if (res.rows.length === 0){
          client.query(insertQuery, (err, res) => {
              const abc = (userLogin(req.cookies.hash_an_app));
              // getting abc as undefined. added async await, promises, setTimeouts. all gives undefined
          })
       }else{
         console.log("username exists");
       }
     })

     res.redirect('/user/'+ 'hello') 

});


Things I've tried:

1.

 const myPromise = new Promise((resolve, reject) => {
    resolve(userLogin(req.cookies.hash_an_app));
 });

 myPromise.then((v)=>{console.log(v) }, (e)=>{console.log(e)});//undefined

 async function asyncAwaitCookies(){
    signedInDetails = await userLogin(req.cookies.hash_an_app)
 }
 asyncAwaitCookies()//undefined
 setTimeout(()=>{console.log(abc)}, 2000);// undefined

Deke
  • 4,451
  • 4
  • 44
  • 65
  • Either promisify `userLogin`, or change it to accept a callback that gets called with `signedInDetails` – CertainPerformance Jul 09 '22 at 19:01
  • you said: `change it to accept a callback that gets called with signedInDetails`. can yo u please show me it in code. i'm stuck. – Deke Jul 09 '22 at 19:13
  • 1
    `function userLogin(cookies, callback){` then near the end `callback(null, signedInDetails)` (sticking to the convention of the first argument being the error) – CertainPerformance Jul 09 '22 at 19:14

0 Answers0