为什么给我“无法读取未定义的属性'deleteProduct'”错误反应

开发5

在react js中删除一行时出现错误。错误是“无法读取未定义的属性'deleteProduct'”。也有任何简单的方法可以使用自定义api从数据库中删除数据。下面是我从数据库中删除数据的完整代码。

这是我删除行的代码-

import React from 'react';
import ReactDOM from 'react-dom';
export default class FetchedData extends React.Component{
  constructor(props){
    super(props);
    this.state={
      UserData:[],
      response: {}
      };
    this.headers=[
      {key:1,label:'Name'},
      {key:2,label:'Department'},
      {key:3,label:'Marks'},
    ];
    this.deleteProduct=this.deleteProduct.bind(this);
  }

  componentDidMount(){
  this.lookupInterval = setInterval(() => {
    fetch("https://www.veomit.com/test/zend/api/fetch.php")
    .then(response => {
                return response.json();
            })
    .then(result => {
                this.setState({
                    UserData:result
                })
        .catch(error => {
        console.log(
          "An error occurred while trying to fetch data from Foursquare: " +error
        );
      });
    });
  }, 500)
  }

  deleteProduct(userId) {
    const { UserData } = this.state;

    const apiUrl = 'https://www.veomit.com/test/zend/api/delete.php';
    const formData = new FormData();
    formData.append('userId', userId);

    const options = {
      method: 'POST',
      body: formData
    }

    fetch(apiUrl, options)
      .then(res => res.json())
      .then(
        (result) => {
          this.setState({
            response: result,
            UserData: UserData.filter(item => item.id !== userId)
          });
        },
        (error) => {
          this.setState({ error });
        }
      )
  } 
  render(){
    return(
      <div>
      <table class="table table-bordered">
          <thead>
            <tr>
              {
                        this.headers.map(function(h) {
                            return (
                                <th key = {h.key}>{h.label}</th>
                            )
                        })
                        }
            </tr>
          </thead> 
          <tbody>
              {
                        this.state.UserData.map(function(item, key) {             
                        return (
                                <tr key = {key}>
                                  <td>{item.name}</td>
                                  <td>{item.department}</td>
                                  <td>{item.marks}</td>
                  <td><span onClick={() => this.deleteProduct(item.id)}>Delete</span></td>
                                </tr>
                            )
                        })
                    }
          </tbody>
      </table>
      </div>
    );
  }
}

请帮助我删除此错误。提前致谢。

戴文·特里翁(Davin Tryon)

您的映射功能正在创建新的范围:

this.state.UserData.map(function(item, key) {             
                    return (
                            <tr key = {key}>
                              <td>{item.name}</td>
                              <td>{item.department}</td>
                              <td>{item.marks}</td>
              <td><span onClick={() => this.deleteProduct(item.id)}>Delete</span></td>
                            </tr>
                        )
                    })

使它成为箭头函数应该可以解决此问题:

this.state.UserData.map((item, key) => {             
                    return (
                            <tr key = {key}>
                              <td>{item.name}</td>
                              <td>{item.department}</td>
                              <td>{item.marks}</td>
              <td><span onClick={() => this.deleteProduct(item.id)}>Delete</span></td>
                            </tr>
                        )
                    })

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我有一个错误“无法读取未定义的属性”地图”?

反应组件错误无法读取未定义的属性

反应-设置状态以显示形式的验证错误消息导致:无法读取未定义的属性'setState'-为什么?

为什么:无法读取未定义的属性“电子邮件”?反应域

TypeError:无法读取未定义反应错误的属性“名称”

为什么我的JavaScript函数不执行?错误:无法读取未定义的属性“更改”

反应错误:无法读取未定义的属性“地图”

我为什么会收到此错误:Uncaught TypeError:无法读取未定义的属性“ john”

带有 ngx-bootstrap 的模态给我错误:无法读取未定义的属性“显示”

反应-类型错误:无法读取未定义的属性“修剪”

反应错误 - 无法读取未定义的属性“状态”

为什么我收到错误“未捕获的类型错误:无法读取 HTMLLIElement 处未定义的属性‘样式’。”?

反应:类型错误:无法读取未定义的属性“值”

类型错误:无法读取未定义反应的属性“状态”

为什么即使我有空列表的回退,也会返回错误“无法读取未定义 [] 的属性“汽车”?

反应:类型错误:无法读取未定义的属性“原型”

为什么反应抛出错误无法读取未定义的属性?

为什么反应返回类型错误:无法正确读取未定义的“加入”?

类型错误:无法读取未定义的属性“setValues” | 反应

类型错误:无法读取未定义反应的属性“setState”

为什么我面临错误 [Vue 警告]:渲染错误:“TypeError:无法读取未定义的属性‘名称’”

类型错误:无法读取未定义的属性“地图”。反应

为什么在定义数据时会出现“无法读取未定义的属性”错误?

为什么我收到错误:类型错误:无法读取未定义的“拆分”的属性

类型错误:无法读取未定义的属性“地图”-反应

为什么我收到错误 - TypeError:无法读取未定义的属性“名称”

为什么我会收到“类型错误:无法读取未定义的属性‘发送’”?

反应 - 错误:TypeError:无法读取未定义的属性(读取“then”)

为什么无法读取未定义错误的属性?