How to get value from Response

Asked

Viewed 2,882 times

0

I’m at an impasse when it comes to getting a JSON of my return that I can’t read it in any way I’ve tried in several ways I will show below how the code is now and what I need to do.

My Component method where is returning what I want.

  onSubmit(loginUsuario, senhaUsuario) {

    this.usuarioService.login(loginUsuario, senhaUsuario).subscribe(
       data => {
         if (data[0].res.status == 200) {

           this.usuario : Usuario = new Usuario(data[0].res._body.code, data[0].res._body.message)

           this.toastr.success("Teste", this.usuario.message);
         } else if (data[0].res.status == 500) {
           this.toastr.error("Teste", data.toString());
         }
         //data => this.data = data,
         //err  => this.erro = <any>err
       },
       error => {
         console.log(error);
       }
    );
  }
}

In my console typing date[0] I left like this, I need to get the Code and Message:

date[0] {res: Response} res : Response {_body: "{"code":0,"message":"Test"}", status: 200, ok: true, statusText: "OK", headers: Headers, ...} proto : Object

I set up my service like this:

  login(loginUsuario: string, senhaUsuario: string): Observable<Response> {
    return this.http.get(this.apiUrl + '/login/' + loginUsuario + '/senha/' + senhaUsuario)
      .map((res:Response) => res.json())
      .catch((error:any) => Observable.throw(error.json().error || 'Server error'));
  }

It worked but I need it to get here in the status:

  onSubmit(loginUsuario, senhaUsuario) {

    this.usuarioService.login(loginUsuario, senhaUsuario).subscribe(
       data => {
         //if (data[0].res.status == 200) {

           //this.usuario : Usuario = new Usuario(data[0].res._body.code, data[0].res._body.message)

           this.toastr.success("Teste", data.message);
         //} else if (data[0].res.status == 500) {
           //this.toastr.error("Teste", data.toString());
         //}
         //data => this.data = data,
         //err  => this.erro = <any>err
       },
       error => {
         console.log(error);
       }
    );
  }

Solution:

My Component:

  onSubmit(loginUsuario, senhaUsuario) {

    this.usuarioService.login(loginUsuario, senhaUsuario).subscribe(
      data => {

        if (data.status == 200) {
          this.data = data.json();
          this.toastr.success("Teste", this.data.message);
        }
      },
      error => {
        console.log(error);
      }
    );
  }

My Service:

  login(loginUsuario: string, senhaUsuario: string): Observable<Response> {
    return this.http.get(this.apiUrl + '/login/' + loginUsuario + '/senha/' + senhaUsuario)
      .map((res:Response) => res)
      .catch((error:any) => Observable.throw(error.json().error || 'Server error'));
  }
  • The same way you’re getting the status.

  • does not work friend, gives Undefined

  • if I try to do this gives Undefined too, this.toastr.Success("Test", data[0].res._body.message);

1 answer

1

JSON parse was missing.

onSubmit(loginUsuario, senhaUsuario) {

this.usuarioService.login(loginUsuario, senhaUsuario)
  .map(data => data.json()) # <---- Transformar texto JSON em objeto!
  .subscribe(data => {
     if (data[0].res.status == 200) {

       this.usuario : Usuario = new Usuario(data[0].res._body.code, data[0].res._body.message)

       this.toastr.success("Teste", this.usuario.message);
     } else if (data[0].res.status == 500) {
       this.toastr.error("Teste", data.toString());
     }
     //data => this.data = data,
     //err  => this.erro = <any>err
   },
   error => {
     console.log(error);
   }
);

}

  • I edited as it was now, but there is one but I need to get the status in my Komponent for me to make some rules, but I managed to get what came now.

  • I was able to finish with your tip, thank you, I posted the solution as it was, now I can continue with my project :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.