【问题标题】:Uncaught TypeError: "text.toLowerCase() Is not a function" [closed]未捕获的 TypeError:“text.toLowerCase() 不是函数”[关闭]
【发布时间】:2018-03-02 23:25:25
【问题描述】:

当我运行下面的代码时,我得到了错误:

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

    const getVisibleExpenses = (expenses, { text, sortBy, startDate, 
      endDate }) => {
    return expenses.filter((expense) => {
    const startDateMatch = typeof startDate !== 'number' || 
      expense.createdAt >= startDate;
    const endDateMatch = typeof endDate !== 'number' || expense.createdAt 
      <= endDate;
    const textMatch = 
      expense.description.toLowerCase().includes(text.toLowerCase());

    return startDateMatch && endDateMatch && textMatch;
  });
};

我不知道出了什么问题。有什么想法吗?

【问题讨论】:

  • 什么是“文本”?
  • 听起来像费用。描述不是一个字符串。您确定该行会引发错误吗?错误显示text.toLowerCase 而不是expense.description.toLowerCase。任何状况之下。只要text 不是字符串的实例,就会专门抛出该错误。
  • var text; (text || '').toLowerCase()
  • toLowerCase() 仅适用于字符串
  • 您发布了更多代码,但仍不是minimal reproducible example。我们无法将其放入浏览器并重现您的问题 - 几乎所有变量都缺少定义。

标签: javascript reactjs function


【解决方案1】:

您必须指定text 的定义是什么。从错误消息中,假设text 持有一个对象,它没有toLowerCase() 方法。

【讨论】:

  • 文本已定义,让我将其余的代码贴出来(我应该首先拥有)。
  • 另外,如果文本是数字类型,它没有 toLowerCase() 方法
猜你喜欢
  • 2014-10-03
  • 2019-12-13
  • 1970-01-01
  • 2017-05-15
  • 2020-04-15
  • 1970-01-01
  • 1970-01-01
  • 2015-08-07
  • 1970-01-01
相关资源
最近更新 更多