Home » Javascript » Alert a parsed responseJson array

Alert a parsed responseJson array

Posted by: admin August 19, 2018 Leave a comment

Questions:

I want to display my array “res” on it’s entirety and parts of it, as an alert.

This is the JSON Postman gives me back.

{"grupos":[{"titulo":"El primero","resena":"este es un grupo"},{"titulo":"Grupo 2","resena":"otro grupo"}],"apellido":"A","mail":"A","nombre":"A","nick":"a"}

I.E
I’ve got Alert.alert(res.mail), and it shows me error 500 red screen.

If i delete the alert line, everythings working smoothly.

On a slightly different topic, is this how you pass the params to another screen?

this.props.navigation.navigate('Router', {email:res.mail});

Thanks a lot for your time!

Edit: This is my fetch code:

fetch(url, {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    username: UserEmail,
    password: UserPassword
  })
})
      .then((response) => response.text())
      .then((responseJson) => {
      if(responseJson.trim() === '"wrong"')
        {
             Alert.alert('E-Mail o Contraseña equivocados')
        }
        else{
          res = JSON.parse(responseJson.trim());
          Alert.alert(res.mail))
          this.props.navigation.navigate('Router', {email:res.mail});
        }
      })
      .catch((error) => {
        console.error(error);
      });
  }
  }
Answers: