uncatch TypeError:无法读取 javascript / React-router / React 中未定

Uncaught TypeError: Cannot read property 'forEach' of undefined in javascript / React-router / React

本文关键字:React-router React javascript TypeError 读取 uncatch      更新时间:2023-09-26

任何人都可以帮助解决java脚本中的此错误吗?我正在使用 React 路由器来显示页面的各种组件。其中一个组件是"forEach"。控制台出现"forEach"属性问题。问题出在哪里?已将此js文件包含在主html文件中。

var ProductTable = React.createClass({
          render: function() {
            var rows = [];
            var lastCategory = null;
            this.props.products.forEach(function(product) {
              if (product.name.indexOf(this.props.filterText) === -1 || (!product.stocked && this.props.inStockOnly)) {
                return ;
              }
              if (product.category !== lastCategory) {
                rows.push(React.createElement(ProductCategoryRow, { category: product.category, key: product.category }));
              }
                rows.push(React.createElement(ProductRow, { product: product, key: product.name }));
                lastCategory = product.category;
            }.bind(this));
            return (
                    React.createElement("table",null,
                        React.createElement("thead",null,
                                React.createElement("tr",null,
                                        React.createElement("th",null,"Name"),
                                        React.createElement("th",null,"Price")
                                    )
                            ),
                            React.createElement("tbody",null,rows)
                    )
            )
          }
        });

您可以使用 defaultProps 来处理此问题,并确保父组件将产品作为数组传递

getDefaultProps: function() {
  return products: []
}