chichi
chichi

Reputation: 3292

Express does not wait for the promise

router.post('/login', async (req, res) => {

  await Auth.findOne({ userEmail }, { userLoginInfo: 0 }).then((auth) => {
    console.log('1');
    console.log(res.header); <---------- header sent after console.log('1')
    // Check if user exists
    if (auth === null) {
      return res
        .status(400)
        .json({ authFailedMessage: 'Email or password is incorrect' });
    } else if (auth !== null) {
      console.log('auuuuuuth')
      console.log(res.header)
      console.log('auuuuuuth13412312')
      bcrypt.compare(password, auth.password).then((isMatch) => {
        if(isMatch) {
          console.log(res.header)
          console.log('matched!')
          console.log(res.header)
        } else if (!isMatch) {
          console.log(res.header)
          console.log('hhhhh?!')
          console.log(res.header)
        }
      })

I am trying to verify users and then sending jwt in cookies. However, the res header fires off even before I do something about the result. I'd tried with new Promise(), however, in this case, it does not move on after the Promise returns the value. How can I send headers after I do something with mongoose query results that have been returned?

------ new code

router.post('/login', async (req, res) => {
  const logInEnvironment = browser(req.headers['user-agent']);
  const ipAddress = requestIp.getClientIp(req);
  const userEmail = req.body.userEmail.toLowerCase();
  const password = req.body.password;
  console.log('aa')
  console.log(res.header);
  // Form validation
  const validation = await validateLoginInput(req.body);
  console.log(res.header);
  console.log('ab')
  // Check validation
  if (!validation.isValid) {
    return res.status(400).json(validation.errors);
  }
  console.log(res.header);
  console.log('ac')
  // Find user by email

  const auth = await Auth.findOne({ userEmail }, { userLoginInfo: 0 })
    console.log(`auuuuth? ${auth}`);
    console.log(res.header);
    console.log('ad')

    if(auth !== null) {
      console.log('ahiuhiuhaisudf!')
    } else {
      console.log('whaitoh!?')
    }
 
});

------ console.log

aa
[Function: header]
a <------ from 'const validation = await validateLoginInput(req.body)'
[Function: header]
ab
[Function: header]
ac
auuuuth? {
  termsandconditions: {
    agreed: true,
  },

}
[Function: header]
ad
ahiuhiuhaisudf!

Upvotes: 0

Views: 90

Answers (1)

Spack Jarrow
Spack Jarrow

Reputation: 360

There's a problem in your code with regards to how you are 'awaiting' the promise. You seem to have appended a .then() to the promise while using the await keyword, they both serve the same purpose.

await Auth.findOne({ userEmail }, { userLoginInfo: 0 }).then((auth) => {

You should try removing the .then() block and store the resolved promise in a variable instead:

const auth = await Auth.findOne({ userEmail }, { userLoginInfo: 0 });
//Do what you want with the 'auth' here

Let me know if this helps.

Upvotes: 1

Related Questions