'Condition on Angular HHTP Request

Can you use conditions, with Angular subscribe function ? Like, in this code :

   this.http.post<any>(phpUrl, content)
.subscribe(
    (r) => {
      if(r.message) // That's how the target script actually manages errors
      { 
        console.log(r.message);
        this.handleErrorPhp(r, formData.email);
      }
      else
      {
        this.router.navigate(['/login'], { queryParams: { register: true } });
      }
    },
    (error) => {
      this.handleErrorPhp(error, formData.email);
    }
  );

I'd like too navigate to the login page, when the script doesn't catch any error. But, even when the "r.message" is empty, my code bugs and stops, telling me r.message is not defined.

EDIT : Here is the console error log :

ERROR TypeError: can't access property "message", i is null


Sources

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

Source: Stack Overflow

Solution Source