Javascript ReactJS toLowerCase 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35248292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:28:43  来源:igfitidea点击:

ReactJS toLowerCase is not a function

javascripthtmlreactjsreactjs-native

提问by MaxWorld

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:

我正在通过将我的文本转换为小写并将其索引与 -1 进行比较来进行比较,以便为 中的特定字段提供一些值ReactJS,但我在 JavaScript 控制台中收到此错误:

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

未捕获的类型错误:props.filterText.toLowerCase 不是函数

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}/>
    });

回答by ThePatelGuy

Looks like Chris's comment is the correct answer:

看起来克里斯的评论是正确的答案:

If titleis a string, use toString()before toLowerCase():

如果title是字符串,则在 toLowerCase() 之前使用toString():

return episode.title.toString().toLowerCase().indexOf(props.filterText.toString().toLowerCase()) > -1;

回答by HoldOffHunger

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 '';
    }
})