0

I'm trying to fetch the youtube video id from the complete url. When i try the following function in codepen.io or in my browser, it works perfectly.

But when i use the same in dialogflow fulfilment it shows the error that match() is not defined where actually it's an inbuilt function.


(Took this solution from here : https://stackoverflow.com/a/8260383/12029508)

Code:

var yt_link = 'https://www.youtube.com/watch?v=GtQstf_sGr4';

function youtube_parser(yt_link) {
  var regExp = `/^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/`;
  var match = yt_link.match(regExp);
  return (match && match[7].length == 11) ? match[7] : false;
}
var videoID = youtube_parser(yt_link);
console.log(videoID);

Error:

dialogflowFirebaseFulfillment
TypeError: Cannot read property 'match' of undefined at youtube_parser (/srv/index.js:304:27) at 
downloadYoutube (/srv/index.js:307:18) at 
WebhookClient.handleRequest (/srv/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:303:44) at 
exports.dialogflowFirebaseFulfillment.functions.https.onRequest (/srv/index.js:498:10) at cloudFunction 
(/srv/node_modules/firebase-functions/lib/providers/https.js:57:9) at /worker/worker.js:783:7 at 
/worker/worker.js:766:11 at _combinedTickCallback (internal/process/next_tick.js:132:7) at 
process._tickDomainCallback (internal/process/next_tick.js:219:9)

Error Screenshot

adiga
  • 34,372
  • 9
  • 61
  • 83
  • `undefined` has no built-in methods. The error message tells you, that `yt_link` is not defined. – Teemu Jun 22 '20 at 11:24
  • Why have you added backticks around regex literal? You don't need that. – adiga Jun 22 '20 at 11:27
  • @Teemu No, yt_link is already defined in 1st line, also if you see the error screenshot, it says match() is not defined. – RUSHABH SHAH Jun 25 '20 at 08:57
  • @adiga I tried without backticks too, it's still throwing the same error. – RUSHABH SHAH Jun 25 '20 at 08:58
  • @RUSHABHSHAH Nope, my first comment still stands. The error message uses a possession construction of English: "_the object of the owner_". Nevertheless what you say, JS says `yt_link` is `undefined`, just log it to the console before use, you'll see. – Teemu Jun 25 '20 at 09:01

0 Answers0