taylorhamcheese taylorhamcheese - 1 year ago 162
Node.js Question

how do I check if a property exists in an object?

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." What does this error mean? Does the if statement require a function? How should I set up this conditional?

function handleApiAiAction(sender, action, responseText, contexts, parameters, response) {
switch (action) {
case "job-search":
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");
//unhandled action, just send back the text
sendTextMessage(sender, responseText);

console.log('parameters') gives me:

{ quickreply: 'quickreply' }

[some edits :)]

Answer Source

{ 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download