0

I am trying to get the status from an API.

As you can see in the image it returns the 423 status, I want to make validations of this form using the status, if status is different from 200 (which mean everything is ok) show the error on the array, if not then pass to the dashboard.

Api response

There is my code from login.component.ts, can you help me with the status and maybe a little with the logic of the sentences to make the validations?

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { LoginService } from './login.service';
import { LoginInt } from 'src/app/Interfaces/login.interfaces';
import { ResponseLInt } from 'src/app/Interfaces/responseL.interface';


@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  FLogin: FormGroup = this.fb.group({ 

email: ['', [Validators.required, Validators.email]],
password: ['', [Validators.required, Validators.minLength(5)]]
  }
    )

  constructor(private fb: FormBuilder, private api:LoginService) { }


  campoValido (campo: string){

    return this.FLogin.controls[campo].errors && this.FLogin.controls[campo].touched

  }


  guardar(form:LoginInt){

    this.api.LoginPass(form).subscribe(data =>{
      console.log(data);            
      ;}
      },

      
      )

    

  ngOnInit(): void {
  }

}

This is my login service

import { Injectable } from "@angular/core";
import { LoginInt } from '../../../Interfaces/login.interfaces';
import { ResponseLInt } from '../../../Interfaces/responseL.interface';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from "rxjs";




@Injectable({

providedIn: 'root'
})
export class LoginService{

    url: string='https://apitest.e-bango.com';


    constructor(private http:HttpClient){}


    LoginPass(form:LoginInt):Observable<ResponseLInt>{
        
        let direccion = this.url + "/api/auth/login"
        
        return this.http.post<ResponseLInt>(direccion,form)
    }
}
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
khriscito
  • 31
  • 1
  • 5
  • please check this https://stackoverflow.com/questions/46019771/catching-errors-in-angular-httpclient – cfprabhu Oct 21 '22 at 10:48

1 Answers1

0

try catchError just like this

this.httpService
        .xxx()
        .pipe(catchError((err: HttpErrorResponse) => of(err)))
        .subscribe(x => {
          if (x instanceof HttpErrorResponse) {
            // do something
          } else {
            // do something
        });
Rick
  • 1
  • 3