2

I am trying to navigate to next page if status code is 200 or response is true. But getting undefined in response.status. If I just log only response I get

{success: true, message: "login successful"}

This below code is getting undefined in console log. I am beginner to Angular and not using any service.

goToHome() {
    console.log(this.login);
    this.Http.post('http://localhost:3000/users/login', this.login).subscribe((response: Response) => {
      console.log(response.status);
      // if (resp.status === 200) {
      //   this.navCtrl.push(TabsPage);
      // } else {
      //   const toast = this.toastController.create({
      //     message: 'User not found please try again',
      //     duration: 2000
      //   });
      //   toast.present();
      // }
    })
  }

2 Answers 2

4

You should add options to your request, and set observe: 'response' to read full response.

this.Http.post('http://localhost:3000/users/login', this.login, { observe: 'response' })
    .subscribe((response) => {
        console.log(response.status);
    })

More info in documentation: Reading the full response

Sign up to request clarification or add additional context in comments.

Comments

4

You should use observe option ({observe: 'response'}) to get a full response that includes the response status code:

goToHome() {
    this.Http.post('http://localhost:3000/users/login', this.login, {observe: 'response'})
        .subscribe((response) => {
            console.log(response.status);
        })
}

2 Comments

I got error, however thanks for help other answer solved my problem ---- Argument of type '(response: Response) => void' is not assignable to parameter of type '(value: HttpResponse<Object>) => void'. Types of parameters 'response' and 'value' are incompatible. Type 'HttpResponse<Object>' is missing the following properties from type 'Response': redirected, trailer, bodyUsed, arrayBuffer, and 4 more. [2345]
Sorry, I have removed the faulty type definition (which causes the error) from the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.