我有以下功能:
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
但是我绑定了外部React对象:
}).bind(this);
那么为什么会出现这种错误?
答案 0 :(得分: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 :(得分: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;
}