title: How to resolve UnhandledPromiseRejectionWarning: TypeError: Cannot read property '0' of undefined
body: <p>I am learning Nodejs and I am trying to implement the <strong>Promise</strong> concept.
  When I run my application I am getting below exception:<br></p>
  <pre><code>Example app listening on port 3000!
  TypeError: Cannot read property 'query' of undefined
      at D:\nodesjapp\rsrest-api\helpers\query.js:10:8
      at new Promise (&lt;anonymous&gt;)
      at module.exports (D:\nodesjapp\rsrest-api\helpers\query.js:1:45)
      at D:\nodesjapp\rsrest-api\app-middlewares\auth.js:30:22
      at processTicksAndRejections (internal/process/task_queues.js:97:5)
  (node:5576) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '0' of undefined
  </code></pre>
  <p>My query helper is <strong>query.js:</strong></p>
  <pre><code>   module.exports = async (conn, q, params) =&gt; new Promise(
      (resolve, reject) =&gt; {
        const handler = (error, result) =&gt; {
            if (error) {
            reject(error);
            return;
          }
          resolve(result);
        }
        conn.query(q, params, handler);
      }).catch(console.log);
  </code></pre>
  <p>I am calling this login API which is written in <strong>auth.js:</strong></p>
  <pre><code> const express = require('express');
      const connection = require('../helpers/connection');
      const query = require('../helpers/query');    
      const router = express.Router();
      const dbConfig = require('../dbConfig');    
      const create = require('../crud/create');
      
      router.post('/login', async (req, res) =&gt; {
        const { username, password } = req.body;
        const conn = await connection(dbConfig).catch(e =&gt; {});
        const user = await query(
          conn,
          `SELECT id, username FROM USERS WHERE username=? AND password=MD5(?)`,
          [username, password]
        );
        res.send(user[0] || { id: null, username: null });
      });    
      module.exports = router;
  </code></pre>
  <p>My database configuration are written in <strong>dbConfig.js:</strong></p>
  <pre><code>const dotenv = require(&quot;dotenv&quot;) 
  dotenv.config();
  // Get the Host from Environment or use default
  const host = process.env.DB_HOST || 'localhost';
  // Get the User for DB from Environment or use default
  const user = process.env.DB_USER || 'root';
  // Get the Password for DB from Environment or use default
  const password = process.env.DB_PASS || 'root';
  // Get the Database from Environment or use default
  const database = process.env.DB_DATABASE || 'twitter_clone';
  module.exports = { host, user, password, database };
  </code></pre>