Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

169
Visualizações
HTTP request status undefined in angular

I want to get the response (status code) of an http request from my node server and if it returns 200, i want to redirect the user. For some reason, res.status is undefined, and I have no idea why

Heres the code for the component:

  submit(){
    this.auth.tryLogin(this.form.getRawValue())
    .subscribe(res => {
      console.log(res);
      if(res.status == 200){
        this.router.navigate(['/'])
      }
      else{
        alert(res.status)
      }
    })
  }

The service:

 tryLogin(data : any){
    return this.http.post(environment.server + environment.routes.authRoutes.login, data, this.options)
  }

And the router for the request:

router.post('/login', async (req, res) => {     //login     TODO: possibly rebuild this function to only return "wrong username or password"
    try{
        let user = await User.findOne({email: req.body.email.toLowerCase()}); //looks for the email address in the db

        if(!user){      //if the user doesnt exist
            return res.status(400).send({
                message: 'user does not exist'
            });
        }
        
        if(!user.isVerified){     //if the user didnt verify his email address
            return res.status(401).send({
                message: 'user is not verified'
            })
        }

        if(!await bcrypt.compare(req.body.password, user.password)){    //if the passwords dont match
            return res.status(400).send({
                message: 'wrong password'
            });
        }
    
        let token = jwt.sign({_id: user._id}, secret);  //jwt token for auth
    
        res.cookie('jwt', token, {  //token gets stored as a cookie
            httpOnly: true,
            maxAge: 24 * 60 * 60 * 1000 //exp time 24 hours'
        });
    
        return res.status(200).send({
            message: 'successfully logged in'
        });
    } catch (e) {
        console.log(e.name + ": " + e.message);
        return res.status(500).send({
            message: 'An error occoured!'
        })
    }
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think you need to set observe property in your http options, the default is body, what you need here is observe response, read the docs here

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda