How to read the result of a JSON Array using Ionic 3?

0

I'm doing a GET to login, however I need to get the user ID for the session too:

submit(){
    var link = 'http://localhost:1337/usuario?email='+ this.usuario.email  + '&senha='+ this.usuario.senha;
    var data = JSON.stringify({ email: this.usuario.email, senha: this.usuario.senha});

      this.http.get(link)
      .subscribe(data => {
        this.data.response = data._body;
       if(this.data.response != "[]"){
         console.log(this.data.response);

        // tinha que pegar o ID do usuário.... :/
         sessionStorage.setItem("usuarioEmail", this.usuario.email);
         sessionStorage.setItem("flagLogado", "sim");
         this.navCtrl.setRoot(WellFitPage, {}, {animate: true, direction: "forward"});
        }else{
          let alert = this.alertCtrl.create({
            title: 'Usuário Não encontrado!',
            subTitle: 'Verifique se digitou seu e-mail e senha corretamente.',
            buttons: ['OK']
          });
          alert.present();
       }
    })
  }

My:

console.log(this.data.response);

Print this result in Console:

[
  {
    "nome": "Ramos",
    "email": "[email protected]",
    "celular": "34996320391",
    "telefone": null,
    "data_nascimento": null,
    "altura": null,
    "peso": null,
    "gordura": null,
    "vo2": null,
    "senha": "teste",
    "cmkey": null,
    "id": 4,
    "createdAt": "2018-03-28T18:08:22.000Z",
    "updatedAt": "2018-03-28T18:08:22.000Z"
  }
]

How to get the id?

    
asked by anonymous 29.03.2018 / 18:47

1 answer

1

Your local variable this.data.response is an array , so you need to access your first position and then use dot notation to access the desired property.

const response = [
  {
    "nome": "Ramos",
    "email": "[email protected]",
    "celular": "34996320391",
    "telefone": null,
    "data_nascimento": null,
    "altura": null,
    "peso": null,
    "gordura": null,
    "vo2": null,
    "senha": "teste",
    "cmkey": null,
    "id": 4,
    "createdAt": "2018-03-28T18:08:22.000Z",
    "updatedAt": "2018-03-28T18:08:22.000Z"
  }
];

console.log(response[0].id);
    
29.03.2018 / 18:56