ReactJS toLowerCase is not a function

2020-08-26 04:04发布

I am doing a comparison by converting my text to lowercase and comparing its index to -1, in order to have some value to the particular field in ReactJS, but I am getting this error in JavaScript console:

Uncaught TypeError: props.filterText.toLowerCase is not a function

var props = this.props;
var rows = this.props.episodes
    .filter(function(episode){
        return episode.title.toLowerCase().indexOf(props.filterText.toLowerCase()) > -1;
    })
    .map(function(episode){
       return <EpisodeRow key={episode.title} episode={episode}/>
    });

2条回答
我欲成王,谁敢阻挡
2楼-- · 2020-08-26 04:09

I think Adam's answer from the comments is actually correct, and turned out to solve my problem :

.filter(function(episode){
    if(episode.title) {
        return episode.title.toLowerCase().indexOf(props.filterText.toLowerCase()) > -1;
    } else {
        return '';
    }
})
查看更多
看我几分像从前
3楼-- · 2020-08-26 04:25

Looks like Chris's comment is the correct answer:

If title is a string, use toString() before toLowerCase():

return episode.title.toString().toLowerCase().indexOf(props.filterText.toString().toLowerCase()) > -1;
查看更多
登录 后发表回答