0

I have database with 2 tables Person and City with relationship One-to-Many

Example of row in Person:

{id: 1, name: 'John', city: 5}

Example of row in City:

{id: 5, name: 'New York'}

In React app, I get values from database Postgresql, save it in state and show in component (table):

...
render() {
 return (
  <table>
    <thead>
      <TableHeader/>
    </thead>
    <tbody>
      {this.props.person.map(item => {
        const city = this.props.city.find(el => el.id === item.city);

        return (<TableRow key={item.id} directory={item} directoryC={city}/>);
      })}
    </tbody>
  </table>
);

In TableRow I add it to table rows:

...
render() {
 return (
   <tr>        
     <td>{this.props.directory.name}</td>
     <td>{this.props.directoryC.name}</td>        
   </tr>
 );
}

This code is works, but in console I see this error:

Uncaught (in promise) TypeError: Cannot read property 'name' of undefined

What happend? How can I fix this error?

Update. Fetch code:

onHandlePersonFetch() {
fetch(`${'127.0.0.1:3000'}/person`, {
  method: 'GET'
})
  .then((response) => {
    if (response.status >= 400) {
      throw new Error('Bad response from server');
    }
    return response.json();
  })
  .then(data =>
    this.setState({ person: data })
  );
  }

  onHandleCityFetch() {
fetch(`${'127.0.0.1:3000'}/city`, {
  method: 'GET'
})
  .then((response) => {
    if (response.status >= 400) {
      throw new Error('Bad response from server');
    }
    return response.json();
  })
  .then(data =>
    this.setState({ city: data })
  );
  }
7
  • It's telling you that there is an error but you didn't catch it... Post your fetch code Commented Oct 20, 2018 at 21:33
  • It looks like the code is working as expected. However, the error seems to happen when your fetching the data from the database (possibly using the fetch method which would explain the TypeError of the Promise). Check the console to figure out which file is throwing undefined. That should give you a better idea of where the problem is originating. Commented Oct 20, 2018 at 21:39
  • @SakoBu add to post Commented Oct 21, 2018 at 7:46
  • @JuanMarco I added to post my fetch code. Please check it, maybe the problem is with this Commented Oct 21, 2018 at 8:00
  • @Jack There you go... Commented Oct 21, 2018 at 9:56

1 Answer 1

1

That is not the proper way to do error handling in promises... Change to this:

 onHandlePersonFetch() {
    fetch(`${'127.0.0.1:3000'}/person`)
      .then(response => response.json())
      .then(data => this.setState({ person: data }))
      .catch(err => console.log(err));
  }

  onHandleCityFetch() {
    fetch(`${'127.0.0.1:3000'}/city`)
      .then(response => response.json())
      .then(data => this.setState({ city: data }))
      .catch(err => console.log(err));
  }

Also, if you are doing async data fetching (which you are...) it should be done within the componentDidMount lifecycle method...

Sign up to request clarification or add additional context in comments.

2 Comments

I call onHandlePersonFetch() and onHandleCityFetch() in ComponentDidMount
If that's the case, then both handlePersonFetch() and handleCityFetch() should return response.json() so that when you call these methods in componentDidMount() you do a setState for both person and city from there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.