TypeError: Невозможно прочитать свойство 'map' из неопределенного (ax ios => getData => setState => .map => return elmItem => error) - PullRequest
1 голос
/ 07 января 2020

Должен ли я использовать нумерацию страниц? Всякий раз, когда я нажимаю эту кнопку <Delete>, ее выбрасывают

TypeError: Невозможно прочитать свойство 'map' из неопределенного.

Есть ли какая-либо связь между

ax ios => getData => setState => .map => return elmItem

Спасибо!

class ContentComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      items: [],
    };
  }

  componentDidMount() {
    axios
      .get("http://localhost:4000/api/todos")
      .then(res => this.setState({ items: res.data.result }))
      .catch(error => console.log(error));
  }

  handleDelete = value => {
    axios
    .delete(`http://localhost:4000/api/todos/${value}`)
    .then(res => this.setState({items: res.data.result}))
    .catch(error => console.log(error));
  };

  render() {
    let { items } = this.state;
    const elmItem = items.map((item, index) => {
      return <TableComponent item={item} key={index} index={index} handleDelete={this.handleDelete}/>;
    });}
    <table className="table">
          <tbody>{elmItem}</tbody>
    </table>

1 Ответ

1 голос
/ 08 января 2020

Отладка функции handleDelete с console.log (); в вашем браузере консоли.

  1. Проверьте, что ваш параметр значения передал правильное значение.
  2. Проверьте свой ответ.
 handleDelete = value => {
    console.log(value); // check paremeter value here
    axios
    .delete(`http://localhost:4000/api/todos/${value}`)
    .then(res => {
      console.log(res.data.result) // check is data return in the array form so you iterate with the map function.
     this.setState({items: res.data.result})
    })
    .catch(error => console.log(error));
  };

Чтобы узнать больше о топоре ios нажмите на ссылку ниже

https://github.com/axios/axios

Чтобы понять о карте, нажмите ссылку ниже

https://reactjs.org/docs/lists-and-keys.html

...