title: NodeJS RESTful API - How to handle 'undefined' request variables properly?
body: <p>I am developing a RESTful API using NodeJS and Express.<br>
  I noticed that incoming requests sometimes lack of some expected variables, which cause the program to crash, saying it couldn't set the value of a variable, to an <code>'undefined'</code> value - as no value arrived with the request.<br>
  Example:<br>
  The application is expecting variableY, but instead variableX is being sent:</p>
  
  <pre><code> formData: { variableX: 'valueX' }
  </code></pre>
  
  <p>The program is expecting to receive variableY, with the following code:</p>
  
  <pre><code>const checkVariables = Joi.validate({ 
      variableY: req.body.variableY,
  }, schema);
  </code></pre>
  
  <p>The application crashes with the following error:</p>
  
  <pre><code>TypeError: Cannot read property 'variableY' of undefined
  </code></pre>
  
  <p>I thought about a few ways to handle that, including declaration of variables upon application initiation and using them along, using <code>try-catch</code>.<br>
  Another way will be to use <code>if-else</code>, <code>if-chaining</code>, or <code>case-switch</code>, but as you understood of course I am looking for the cleanest way to achieve that.<br>
  Any ideas?</p>
  
  <p>Thank you.</p>
  
  <p>** EDIT **<br>
  Progressed and managed to achieve the result using the object only. Once trying to reach any of it's inner fields the error will be thrown anyway, example:<br>
  <code>if(req.body.variableY == undefined){console.log('The expected variable is undefined');} //true</code>  </p>
  
  <p>When the validation addresses a field inside the 'undefined' object:<br>
  <code>if(req.body.variableY.dataId == undefined){console.log('The expected variable is undefined');} //crashes</code><br>
  The following error is being thrown again:<br>
  <code>TypeError: Cannot read property 'variableX' of undefined</code>  </p>
  
  <p>After doing some more digging around, found this Stackoverflow thread:<br>
  <a href="https://stackoverflow.com/questions/11040472/how-to-check-if-object-property-exists-with-a-variable-holding-the-property-name]">How to check if object property exists with a variable holding the property name?</a><br>
  Tried using hasOwnProperty, but the same kind of error is being thrown:<br>
  <code>TypeError: Cannot read property 'hasOwnProperty' of undefined</code></p>
  
  <p>Tried wrapping variable declaration using <code>try-catch</code>, still didn't work:  </p>
  
  <pre><code>try{
      var variableX = req.body.variableX
      var variableXDataId = req.body.variableX.dataId
  }
  catch(e){
      res.status(400).send('Wrong request error: Please check your request variables and try again');
  }
  </code></pre>
  
  <p>As this is a really basic validation that should be addressed by most of the RESTful APIs (validating that you get the expected incoming variables inside the request, so the program won't crash by having errors it can't handle - what is the common solution for such problems (expected / unexpected request validation)?</p>
  
  <p>Thank you.</p>