2016-07-15 83 views
1

任何人都可以在java腳本中幫助解決這個錯誤。我正在使用React路由器來顯示頁面的各種組件。其中一個組件是'forEach'。控制檯提供了「forEach」屬性的問題。問題是什麼 ?已經將這個js文件包含到主html文件中。Uncaught TypeError:無法讀取在javascript/React-router/React中未定義的屬性'forEach'

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) 
        ) 
      ) 
      } 
     }); 
+0

不要你需要創建'createClass'給ForEach元素?檢查此:http://stackoverflow.com/questions/33681136/react-js-foreach-as-a-first-class-component – SrAxi

+0

它告訴你'this.props.products'是'undefined'因此它沒有' forEach'方法。確保在調用render()之前它已被填充 –

+0

拋出的實際錯誤是什麼? – Zargold

回答

0

您可以使用defaultProps來處理這一點,也確保你的父組件傳遞產品作爲數組

getDefaultProps: function() { 
    return products: [] 
} 
相關問題