2

I'm creating a mongoose schema and need to do required checks a lot, so I tried creating this function. But it says that 'subcategory' is initialized but never used. How can I get the function to check this. like I need it to?

function requiredCheck(subcategory, value) {
  return this.subcategory === value;
}
Simca
  • 71
  • 7

1 Answers1

3

Use bracket accessors to check a property of this by variable:

function requiredCheck(subcategory, value) {
  return this[subcategory] === value;
}

Otherwise you're checking for the property with the literal key "subcategory" which probably isn't what you want.

Klaycon
  • 10,599
  • 18
  • 35