2016-04-14 106 views
1

財產「狀態」我有以下功能:遺漏的類型錯誤:無法讀取的不確定

extractCountries: function() { 
    var newCountries = []; 
    _.forEach(this.props.countries, function(country) { 

     var monthTemp = Utils.thisMonthTemp(parseFloat(country["name"]["temperature"])); 

     if(parseFloat(country["name"]["safety"]) > this.state.minSafety && 
      parseFloat(country["name"]["nature"]) > this.state.minNature && 
      this.state.weather.min <= monthTemp && 
      monthTemp <= this.state.weather.max) { 

      newCountries.push(country); 
     } 
    }).bind(this); 
    return newCountries; 
    } 

上線parseFloat(country["name"]["safety"]) > this.state.minSafety &&我得到:

Uncaught TypeError: Cannot read property 'state' of undefined

但我結合外部陣營object:

}).bind(this); 

那麼爲什麼會出現這種錯誤呢?

+3

你不具約束力的功能本身。移動parens內的'bind'調用。目前,您正在對'_.forEach'的返回值調用'bind'。 –

+0

謝謝。這解決了它。 – octavian

回答

1

試試這個

extractCountries: function() { 
     var newCountries = []; 
     _.forEach(this.props.countries, function(country) { 

      var monthTemp = Utils.thisMonthTemp(parseFloat(country["name"]["temperature"])); 

      if(parseFloat(country["name"]["safety"]) > this.state.minSafety && 
       parseFloat(country["name"]["nature"]) > this.state.minNature && 
       this.state.weather.min <= monthTemp && 
       monthTemp <= this.state.weather.max) { 

       newCountries.push(country); 
      } 
     }, this); 
     return newCountries; 
     } 
1

正如評論提到你只需要做到這一點

extractCountries: function() { 
    var newCountries = []; 
    _.forEach(this.props.countries, function(country) { 

     var monthTemp = Utils.thisMonthTemp(parseFloat(country["name"]["temperature"])); 

     if(parseFloat(country["name"]["safety"]) > this.state.minSafety && 
      parseFloat(country["name"]["nature"]) > this.state.minNature && 
      this.state.weather.min <= monthTemp && 
      monthTemp <= this.state.weather.max) { 

      newCountries.push(country); 
     } 
    }.bind(this)); 
    return newCountries; 
    } 
相關問題