Home > Mobile >  location.href is not working in server.js NodeJS
location.href is not working in server.js NodeJS

Time:10-08

In the below code, I am trying to send it to the login page but it is not working.

app.get('/current-pass', (req, res) => {
res.sendFile(path.join(staticPath, "currentPass.html"));
})

app.post('/current-pass', (req, res) => {
let { email, cpassword } = req.body;

if(cpassword.value){
  return res.json({ 'alert': "please enter your current password"});
}

db.collection('users').doc(email).get()
.then(user => {
  bcrypt.compare(cpassword, user.data().password, (err, result) => {
    if(result){
      let data = user.data();
      return res.json({
        location.href = "/login"; // This is not working
      })
    } else{
      return res.json({'alert': 'current password is incorrect'});
    }
  })
})

})

All of the code is working but location.href is not working. In the place of,

return res.json({
 location.href = "/login";
})

I used,

return res.send('success');

But it is also not working

CodePudding user response:

You can use res.redirect('/login'); to redirect with Express.js. There is no location variable in a request.

  • Related