taylorhamcheese taylorhamcheese - 5 days ago 6
Javascript Question

Conditional for if is throwing "parameters.includes is not a function"

Apologize. I'm fairly certain this is a rather novice question; however, here it goes! Thanks for any hints in advance :)

I have the below function in my app, and running the app now throws the error "TypeError: parameters.includes is not a function"

function handleApiAiAction(sender, action, responseText, contexts, parameters, response) {
switch (action) {
case "job-search":
console.log(parameters);
if (parameters.includes("quickreply")) {
console.log("it worked");
} else {
let replies = response.result.fulfillment.messages[0].replies;
let title = response.result.fulfillment.messages[0].title;
sendQuickReply(sender, title, replies);
console.log("it didn't work");
}
break;
default:
//unhandled action, just send back the text
sendTextMessage(sender, responseText);
}
}


console.log('parameters') gives me:

{ quickreply: 'quickreply' }

Answer

{ quickreply: 'quickreply' } does not contain the object member includes: function() {} (nor does the Object.prototype).

What it looks like you're trying to do is check if the object contains the property quickreply, so you should do something like this:

if('quickreply' in parameters) {
  // do stuff
}

There are other methods you can use, too, like hasOwnProperty. They each have their own nuances, but in looks like it should work in your case.

Comments