title: How to fix "Cannot read property 'match' of undefined" error in JavaScript?
body: <p>I'm new to Node JS and am trying to set up a basic application that fetches my inbox (outlook) contents in JSON format. I tried using match function to filter the data based on subject but I keep getting this error - </p>
  
  <pre><code>TypeError: Cannot read property 'match' of null
      at WriteStream.&lt;anonymous&gt; (C:\Users\52025088\Desktop\EMAIL APPROVAL DEMO\Outlook NODE\OFFICE365_NJS_DEMO.js:44:29)
      at Object.onceWrapper (events.js:273:13)
      at WriteStream.emit (events.js:182:13)
      at lazyFs.open (internal/fs/streams.js:278:10)
      at FSReqWrap.oncomplete (fs.js:141:20)
  </code></pre>
  
  <p>I tried using other string functions as well but keep getting similar errors.</p>
  
  <p><strong>I am able to print 'json.value[i].Subject' in console but cannot use it in match function.</strong></p>
  
  <p>/<em>Request portion</em>/</p>
  
  <pre><code>request(options, function(err, res, body) {  
  let json = JSON.parse(body);
  
  console.log(err);
  
  var fileName = 'abc.html';
  var html = '';
  var stream = fs.createWriteStream(fileName);
  
  
  stream.once('open', function(fd) {
  for (var i = 0; i &lt; json.value.length-1; i++){
  
  
  
  
  
          if(json.value[i].Subject.match(/Action Required/)){
  
              var dom = new JSDOM(json.value[i].Body.Content);
  
              console.log(json.value[i].Subject);
  
              html = html + '&lt;br/&gt;' + json.value[i].Subject + '&lt;a href=' + dom.window.document.querySelector("a") + 'target="_top"&gt;Approve&lt;/a&gt;&lt;br/&gt;&lt;br/&gt;';
  
          }       
  }
  html = '&lt;!DOCTYPE html&gt;'
     + '&lt;html&gt;&lt;head&gt;' + "List Of Pending Items" + '&lt;/head&gt;&lt;body&gt;' + html + '&lt;/body&gt;&lt;/html&gt;';
  stream.end(html);
  });
  </code></pre>
  
  <p>});</p>
  
  <pre><code>C:\Users\52025088\Desktop\EMAIL APPROVAL DEMO\Outlook NODE&gt;node OFFICE365_NJS_DEMO.js
  null
  FW: Action Required: Approval of Earned Leave Absence Request for Vishnu Babu from 2019-03-26 to 2019-03-26
  FW: Action Required: Approval of Earned Leave Absence Request for Vishnu Babu from 2019-05-08 to 2019-05-08
  C:\Users\52025088\Desktop\EMAIL APPROVAL DEMO\Outlook NODE\OFFICE365_NJS_DEMO.js:44
                          if(json.value[i].Subject.match(/Action Required/)){
                                                   ^
  
  TypeError: Cannot read property 'match' of null
      at WriteStream.&lt;anonymous&gt; (C:\Users\52025088\Desktop\EMAIL APPROVAL DEMO\Outlook NODE\OFFICE365_NJS_DEMO.js:44:29)
      at Object.onceWrapper (events.js:273:13)
      at WriteStream.emit (events.js:182:13)
      at lazyFs.open (internal/fs/streams.js:278:10)
      at FSReqWrap.oncomplete (fs.js:141:20)
  </code></pre>
  
  <p>I expect the match function to work so that I can filter based on Subject (json.value[i].Subject) but I guess I missed out something.</p>
  
  <p>However I am able to get results if I hard code some string to compare with the Subject.</p>
  
  <pre><code>if(json.value[i].Subject == 'FW: Action Required: Approval of Earned Leave Absence Request for Vishnu Babu from 2019-03-26 to 2019-03-26'){
  
                  var dom = new JSDOM(json.value[i].Body.Content);
  
                  console.log(json.value[i].Subject);
  
                  html = html + '&lt;br/&gt;' + json.value[i].Subject + '&lt;a href=' + dom.window.document.querySelector("a") + 'target="_top"&gt;Approve&lt;/a&gt;&lt;br/&gt;&lt;br/&gt;';
  
              }
  
  C:\Users\52025088\Desktop\EMAIL APPROVAL DEMO\Outlook NODE&gt;node OFFICE365_NJS_DEMO.js
  null
  FW: Action Required: Approval of Earned Leave Absence Request for Vishnu Babu from 2019-03-26 to 2019-03-26
  </code></pre>