-2

Uncaught TypeError: Cannot read property '0' of undefined at handleResults (Script1.js:17) at SpeechRecognition.recog.onresult (Script1.js:11)

Javascript:

const speechRecognition =
  window.webkitSpeechRecognition /*Chrome*/ ||
  window.SpeechRecognition; /*Firefox...*/

function startListening() {
  const recog = new speechRecognition
  recog.start();
  recog.onstart = console.log("Started Listening..");

  recog.onresult = function(data) {
    handleResults(data);
  }
}
//'data' comes from 'onresult'

function handleResults(data) {
  let text = data.result[0][0].transcript;
  text = text.toLowerCase();

  if (text.includes('instagram')) {
    console.log("Opening Instagram..");
    window.open("https://www.instagram.com");
  }

}

// Call Function On Load

window.addEventListener('DOMContentLoaded', startListening());
Anoynomous
  • 27
  • 4
  • 1
    Not really related but your last line should be `window.addEventListener('DOMContentLoaded', startListening)` (no `()` after `startListening`) – Phil Aug 03 '21 at 03:43
  • Have you tried doing any debugging? Inspect the value of `data` as its `result` property doesn't appear to be a 2-dimensional array – Phil Aug 03 '21 at 03:44

1 Answers1

-1

Just change the result call to results:

let text = data.results[0][0].transcript;
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Anoynomous
  • 27
  • 4