'Getting Promise { <pending> } while using passport.js, bcrypt and sqlite3

this the relevant code in my app.js

async function getusername(username) {

  const pro1 =await  new Promise((resolve, reject) => {
    sqlDB.all(sql, [username], (err, rows) => {
      if (err) {
        // promise needs to be rejected as an error occurred
        reject(err)
      }
      if (!(rows.length === 0) && username === rows[0].username) {
        console.log(username + " sqn " + rows[0].username);
        // console.log(rows[0]);
        currentUsernameObj = rows[0];
      } else {
        console.log("user not found");
      }
      resolve(currentUsernameObj);
    });
  });
  return pro1;
}
async function getid(id) {
  const pro2 = await new Promise((resolve, reject) => {
    sqlDB.all(sql2, [id], (err, rows) => {
      if (err) {
        reject(err);
      }
      if (!(rows.length === 0) && id === rows[0].id) {
        console.log(id + " sqn " + rows[0].id);
        console.log(rows[0]);
        currentIdObj = rows[0];

      } else {
        console.log("userid not found")
      }
      resolve(currentIdObj);
    })
  });
  return pro2;
};
// in order to be able to call an async method with await the call has to be in an async method itself
(async () => {

  
  initializePassport(
    passport,
    async function (username) {
      let x = await getusername(username)
      return await x;
    },
    async function (id) {
      let y = await getid(id)
      return y;
    }
  )

})();

app.post('/login', passport.authenticate("local", {
  successRedirect: '/secrets',
  failureRedirect: '/login',
  failureFlash: true

}));

this is the code in my passport-config file

 const LocalStrategy = require('passport-local').Strategy
 const bcrypt = require('bcrypt');


function  initializePassport(passport,getUserByUsername,getUserById){ 

    async function authenticateUser(username,password,done){
        const user = getUserByUsername(username)
        if(user==null){
            return done(null,false,{ message: 'No user with that username' })
        }
        else{
            console.log(password + " user.pass : "+ user.password);
            console.log(user);

        }
        try {
            if(
                
                await bcrypt.compare(password, user.password)){
                return done(null, user)
            }
            else{
                return done(null, false, { message: 'Password incorrect' })
            }
        } catch (e) {
            return done(e)
            
        }

    }
    passport.use(new LocalStrategy({ usernameField: 'username' }, authenticateUser))
    passport.serializeUser(function(user, done) {
        done(null, user.id);
      });
      
    passport.deserializeUser(function(id, done) {
        return done(null, getUserById(id));
      });
}
module.exports= initializePassport

this is my output enter image description here

the error is coming in at the initializePassport which the goes into the passport-config and then we get the error when bcrypt trys to compare the password because user is a pending promise.I believe i have put the promises and await properly yet i am getting a promise pending(as seen in the console log), i think it's because initializePassport is being called before the data is fetched from the db. I dont know how to make it wait for that. I tried putting a .then after calling getusername() but that didnt seem to work either



Solution 1:[1]

getUserByUsername is a function that always returns a promise, so it is normal that user is a promise object. You didn't get the resolved value from the promise. Do const user = await getUserByUsername(username) – trincot

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jagrat