Undefined is not an object (evaluating 'items.reduce)

Viewed 679

Here my codes :

Voice.onSpeechStart = this.onSpeechStart.bind(this);
Voice.onSpeechRecognized = this.onSpeechRecognized.bind(this);
Voice.onSpeechResults = this.onSpeechResults.bind(this);}

componentWillUnmount() {
    Voice.destroy().then(Voice.removeAllListeners);}

  onSpeechStart(e) {
    this.setState({
      started: "√"}
);}

  onSpeechRecognized(e) {
    this.setState({
      recognized: "√"
    });
  }

  onSpeechResults(e) {
   this.setState({results: e.value});

   if(matchSorter(this.setState.results,'login now') === 'login now'){
        this.props.navigation.navigate("Dashboard");
   }
   else {
        ToastAndroid.show("Please Try Again", ToastAndroid.SHORT);
   }
  }

async _startRecognition(e) {
this.setState({
  recognized: "",
  started: "",
  results: []
});
try {
  await Voice.start("en-US");
} catch (e) {
  console.error(e);
}


 render() {
    return (
    <Text>Say "Login now"</Text>

    <TouchableOpacity
      onPress={this._startRecognition.bind(this)}>
    <Text> TAP HERE </Text>}

and this error coming out :

Error Image from Android-based Mobile:

enter image description here

*The output of "this.setState.results" when I say login now is "blogging nowlogging nowlogin nowjogging nowlog in now".

1 Answers

When you tried to retrive value from state then you have to write code like : this.state.VARIBLENAME. You have this.setState.VARIBLENAME which is wrong.

Correction from your code:

   if(matchSorter(this.state.results,'login now') === 'login now'){
        this.props.navigation.navigate("Dashboard");
   }

Your whole code:

Voice.onSpeechStart = this.onSpeechStart.bind(this);
Voice.onSpeechRecognized = this.onSpeechRecognized.bind(this);
Voice.onSpeechResults = this.onSpeechResults.bind(this);}

componentWillUnmount() {
    Voice.destroy().then(Voice.removeAllListeners);}

  onSpeechStart(e) {
    this.setState({
      started: "√"}
);}

  onSpeechRecognized(e) {
    this.setState({
      recognized: "√"
    });
  }

  onSpeechResults(e) {
   this.setState({results: e.value});

   if(matchSorter(this.state.results,'login now') === 'login now'){
        this.props.navigation.navigate("Dashboard");
   }
   else {
        ToastAndroid.show("Please Try Again", ToastAndroid.SHORT);
   }
  }

async _startRecognition(e) {
this.setState({
  recognized: "",
  started: "",
  results: []
});
try {
  await Voice.start("en-US");
} catch (e) {
  console.error(e);
}


 render() {
    return (
    <Text>Say "Login now"</Text>

    <TouchableOpacity
      onPress={this._startRecognition.bind(this)}>
    <Text> TAP HERE </Text>
}

You can replace the code and it will work for you.

Related