Home  >  Q&A  >  body text

React Native: Undefined response issue encountered when accessing localhost Symfony API

I have an app built using React Native that is running on my local computer and I want to get and display data from my local Symfony API that is running.

The React Native code is getting from my local PC IP and Symfony port/route:

constructor(props) {
  super(props);
  this.state = {
    isLoading: true,
    dataSource: [],
  }
}

componentDidMount() {
  return fetch('http://10.161.170.86:8000/api/hardwarePlacement')
  .then((response) => {
    console.log(response.ok);
  })
  .then((response) => response.json())
  .then((responseJson) => {
    console.log(response.ok);
    this.setState({
      isLoading: false,
      dataSource: responseJson.hardwarePlacements,
    })
  })
  .catch((error) => {
    console.log(error)
  });
}

The JSON data I get from my Symfony API is as follows, when I get it through Postman or directly through the browser:

[{"id":1,"name":"Bryggers","createdDate":{"date":"2023-02-08 15:14:12.000000","timezone_type":3,"timezone":"Europe\/Berlin"},"editedDate":{"date":"2023-02-14 13:57:07.000000","timezone_type":3,"timezone":"Europe\/Berlin"}},{"id":2,"name":"Stue","createdDate":{"date":"2023-02-08 21:52:46.000000","timezone_type":3,"timezone":"Europe\/Berlin"},"editedDate":{"date":"2023-02-08 21:52:46.000000","timezone_type":3,"timezone":"Europe\/Berlin"}},{"id":3,"name":"Stue","createdDate":{"date":"2023-02-14 13:57:10.000000","timezone_type":3,"timezone":"Europe\/Berlin"},"editedDate":{"date":"2023-02-14 13:57:10.000000","timezone_type":3,"timezone":"Europe\/Berlin"}}]

The error I get in the terminal is:

[TypeError: undefined is not an object (evaluating 'response.json')]

If I try to get the data from the public URL it works fine, only when getting the data from the localhost URL it fails.

P粉094351878P粉094351878422 days ago565

reply all(1)I'll reply

  • P粉333186285

    P粉3331862852023-09-14 11:29:53

    Return from the first then

    .then((response) => {
        console.log(response.ok);
        return response
      })  
      .then((response) => response.json())

    reply
    0
  • Cancelreply