Javascript Expo/React本机错误-TypeError:undefined不是对象(计算';string.toLowerCase';)

Javascript Expo/React本机错误-TypeError:undefined不是对象(计算';string.toLowerCase';),javascript,android,react-native,expo,Javascript,Android,React Native,Expo,所以我正在制作一个React原生应用程序,我在heroku上安装了服务器和数据库。 使用expo在手机上和我的pc上的web浏览器中查看 在网络上,一切正常 在电话里,我从截图中得到了错误。我想是因为数据不及时出现了问题,或者是因为写东西的方式不同 有没有一种不用android studio就能在手机上获得完整应用程序的方法 以下是与错误相关的代码: <TextInput style={styles.input} placeholder='Search...' o

所以我正在制作一个React原生应用程序,我在heroku上安装了服务器和数据库。 使用expo在手机上和我的pc上的web浏览器中查看

在网络上,一切正常

在电话里,我从截图中得到了错误。我想是因为数据不及时出现了问题,或者是因为写东西的方式不同

有没有一种不用android studio就能在手机上获得完整应用程序的方法

以下是与错误相关的代码:

<TextInput
    style={styles.input}
    placeholder='Search...'
    onChange={(e) => { this.handleInput(e.target.value) }}
></TextInput>


handleInput = (value) => {
  if (value !== this.state.insertionIngredient) {
    this.setState({ ...this.state, insertionIngredient: value });
  };
};

handleFindButton = () => {
  this.setState({ ...this.state, isLoading: true });

  const id = searchString(this.state.insertionLanguage, this.state.data, 
                          this.state.insertionIngredient);
  const result = getById(this.state.queryLanguage, this.state.data, id);

  if (id === 'No such word in the database.' || result === 'No such word in the database.') {
    this.setState({ ...this.state, queryIngredient: 'No such word in the database.' });
  } else {
    this.setState({ ...this.state, queryIngredient: result });
  };
};

const searchString = (language, state, string) => {
  let array;
  if (language === 'english') {
      array = state.english;
  } else if (language === 'spanish') {
      array = state.spanish;
  } else if (language === 'german') {
      array = state.german;
  } else if (language === 'serbian') {
      array = state.serbian;
  };

  string = string.toLowerCase();
  let filter = escapeRegExp(string);
  let regex = new RegExp("^" + filter, "i");

  const word = array.filter(val => {
      return val.word.match(regex)
  });

  if (word.length > 0) {
      return word[0].id;
  } else {
      return 'No such word in the database';
  }};
{this.handleInput(e.target.value)}
>
handleInput=(值)=>{
if(值!==this.state.insertionCredit){
this.setState({…this.state,insertionCredit:value});
};
};
handleFindButton=()=>{
this.setState({…this.state,isLoading:true});
const id=searchString(this.state.insertionLanguage、this.state.data、,
此。状态。插入元素);
const result=getById(this.state.queryLanguage,this.state.data,id);
if(id=='数据库中没有这样的单词。| | result=='数据库中没有这样的单词'){
this.setState({…this.state,QueryCredit:'数据库中没有这样的词');
}否则{
this.setState({…this.state,querycredit:result});
};
};
const searchString=(语言、状态、字符串)=>{
let数组;
如果(语言==‘英语’){
数组=state.english;
}else if(语言===‘西班牙语’){
数组=state.西班牙语;
}else if(语言==‘德语’){
数组=state.derman;
}else if(语言==‘塞尔维亚语’){
数组=state.serbian;
};
string=string.toLowerCase();
let filter=escapeRegExp(字符串);
设regex=newregexp(“^”+过滤器,“i”);
const word=array.filter(val=>{
返回val.word.match(正则表达式)
});
如果(字长>0){
返回字[0].id;
}否则{
返回“数据库中没有此类单词”;
}};

仅从阅读错误来看,我相信您遇到了
字符串未定义的情况。一个简单的解决方法是检查
string
是否存在,如果存在,则对其应用小写。例如:

if(string)string=string.toLowerCase()


else console.log(“字符串不存在,请查看”)

问题是我使用了onChange方法而不是onChangeText。因此,与此相反:

<TextInput
  style={styles.input}
  placeholder='Search...'
  onChange={(e) => { this.handleInput(e.target.value) }}
></TextInput>
{this.handleInput(e.target.value)}
>
我应该说:

<TextInput
  style={styles.input}
  placeholder='Search...'
  onChangeText={(e) => { this.handleInput(e) }}
></TextInput>
{this.handleInput(e)}
>

由于可用的代码和快照有限,我只能怀疑错误是否与具有不同值的state
InsertionCredit
有关。我建议插入两条日志语句来跟踪所有函数体中
insertioncredit
的值。另外,不要将参数传递给
searchString(this.state.insertionLanguage、this.state.data、this.state.insertionCredit)
。。我建议,调用
searchString()
,不使用任何参数,并从状态访问语言、状态和字符串值。我可以尝试将代码带回主组件,而不是导入它,但我认为这不会解决问题。不,我的意思是,仅将代码保留在组件中,但可能会更改逻辑布局的方式。看起来是线性的代码块实际上并不是串行执行,而是并行执行所有行。我的观点是,
状态
对于
InsertionCredit
的值可能与我们预期的值不同。我知道这就是问题所在,但字符串不能为空,因为它是在使用onChange方法更改输入值时生成的。只有在我手机上的expo broadcasting app导致延迟的情况下,才能对其进行未定义。为了调试,您是否可以尝试使用
onChangeText()
方法而不是
onChange()
。在您的示例中:
{this.handleInput(userInput)}>