0

我无法弄清楚为什么这会作为错误返回。

item.Question.toUpperCase 不是函数

我在我的 expo 应用程序中使用 react 收到此错误,但在控制台中没有抛出任何错误

错误:

TypeError: item.Question.toUpperCase 不是函数。initem.Question.toUpperCase() item.Question.toUpperCase未定义

我尝试创建新变量为

var Question = item.Question.toUpperCase() 

但这似乎不起作用所以这里是完整的代码

else if(item.Title == undefined){
        const itemData = `${item.Question.toUpperCase()} ${item.answer.toUpperCase()} ${item.keyword.toUpperCase()}`;
        const textData = search.toUpperCase();

        return itemData.indexOf(textData) > -1;

item 是一个对象,其中 Question 是一个字符串

谢谢

编辑:所以经过一些记录后,我发现这个区域给出了错误:

console.log(item.keyword,item.Title)
        console.log(typeof item.Question, item.Question)
        const itemData = `${item.Title.toUpperCase()} ${item.Question.toUpperCase()} ${item.answer.toUpperCase()} ${item.keyword.toUpperCase()}`;
        const textData = search.toUpperCase();
        console.log(typeof itemData,typeof textData)

当记录 item.Question 和我得到的数字和 NaN 类型时,这很奇怪,因为它应该从我的数据库中提取字符串“这是一个 strng10” 对数据库的所有其他调用似乎都工作正常。他们为 item.Question 提供了正确的类型作为字符串。

所以我想我要解决的问题是如何将字符串转换为 NaN 的数字?

4

2 回答 2

0

这似乎对我有用。你确定你传递了正确的数据类型吗?

let item = {
  Question: "test",
  Title: undefined,
  Answer: "thing",
  Keyword: "wow"
}

if (item.Title === undefined) {
  const itemData =
      `${item.Question.toUpperCase()} ${item.Answer.toUpperCase()} ${item.Keyword.toUpperCase()}`
  itemData
}

控制台返回"TEST THING WOW"

于 2020-05-16T08:02:29.293 回答
0

因此,在一直记录到我对数据库的调用之后,我在收到问题之前发现了一个额外的 +,删除它解决了问题。很抱歉浪费了你所有的时间。

问题在对象内部:

Question: + childData.Question
于 2020-05-16T17:35:35.067 回答