title: NodeJS - How to deal with UnhandledPromiseRejectionWarning?
body: <p>I am fairly new to Node JS and so I am struggling a bit.</p>
  
  <p>I am trying to read files for google drive using their API, from my Node Js code.</p>
  
  <p>I am having the following code</p>
  
  <pre><code>router.get('/', function (req, res, next) {
  
    var pageToken = null;
  // Using the NPM module 'async'
  async.doWhilst(function (callback) {
    drive.files.list({
      q: "mimeType='image/jpeg'",
      fields: 'nextPageToken, files(id, name)',
      spaces: 'drive',
      pageToken: pageToken
    }, function (err, res) {
      if (err) {
        // Handle error
        console.error(err);
        callback(err)
      } else {
        res.files.forEach(function (file) {
          console.log('Found file: ', file.name, file.id);
        });
        pageToken = res.nextPageToken;
        callback();
      }
    });
  }, function () {
    return !!pageToken;
  }, function (err) {
    if (err) {
      // Handle error
      console.error(err);
    } else {
      // All pages fetched
    }
  })
  
    res.render('index', { title: 'Express' });
  
  });
  </code></pre>
  
  <p>The above code is giving me the following error when i send the get request</p>
  
  <pre><code>(node:13884) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined
      at E:\nodejs\newcelebapi\routes\index.js:49:17
      at E:\nodejs\newcelebapi\node_modules\googleapis-common\build\src\apirequest.js:43:53
      at processTicksAndRejections (internal/process/task_queues.js:97:5)
  (node:13884) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
  (node:13884) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
  </code></pre>
  
  <p>The issue is in the following line </p>
  
  <pre><code>  res.files.forEach(function (file) {
  </code></pre>
  
  <p>I've tried everything I could and gave up understanding the problem.</p>
  
  <p>Could you please help me with this?</p>
  
  <p>Thanks!</p>