title: Node Express js Unhandled rejection Error [ERR_HTTP_HEADERS_SENT]
body: <p>I am new for node js. I am trying to insert the data to database, before that I did some validation for checking username already exist in database. Please see the coding which is used by me.</p>
  
  <pre><code>var express = require("express");
  var router = express.Router();
  var models = require("../models");
  
  const { check, validationResult } = require('express-validator/check');
  
  /* GET users listing. */
  router.get('/', function(req, res, next) {
      res.send('respond with a resource - merchant');
  });
  
  /* POST ADD USERS. */
  router.post('/add-merchant', [
      check('name').not().isEmpty(),
  ], (req, res, next) =&gt; {
      try {
          const errors = validationResult(req);
  
          if (!errors.isEmpty()) {
              return res.json({ errors: errors.array()});
          }
  
          console.log("error passed")
  
          models.merchants.findOne({ where: {name: req.body.name}}).then((merchant) =&gt; {
              if(merchant) {
                 throw new Error("Username already exists")
              }
              models.merchants.create(req.body).then((merchant) =&gt; {
                  res.json(merchant)
              });
          })
      } catch (error) {
          res.json({"status": "error", "message": error.message})
      }
  });
  
  module.exports = router;
  </code></pre>
  
  <p>I am getting below error while running the above code </p>
  
  <pre><code>Unhandled rejection Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
      at ServerResponse.setHeader (_http_outgoing.js:470:11)
      at ServerResponse.header (/home/assistanz/Desktop/node/gb360/node_modules/express/lib/response.js:767:10)
      at ServerResponse.send (/home/assistanz/Desktop/node/gb360/node_modules/express/lib/response.js:170:12)
      at ServerResponse.json (/home/assistanz/Desktop/node/gb360/node_modules/express/lib/response.js:267:15)
      at models.merchants.create.then (/home/assistanz/Desktop/node/gb360/routes/merchant.js:33:21)
      at tryCatcher (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/util.js:16:23)
      at Promise._settlePromiseFromHandler (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/promise.js:512:31)
      at Promise._settlePromise (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/promise.js:569:18)
      at Promise._settlePromise0 (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/promise.js:614:10)
      at Promise._settlePromises (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/promise.js:694:18)
      at _drainQueueStep (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/async.js:138:12)
      at _drainQueue (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/async.js:131:9)
      at Async._drainQueues (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/async.js:147:5)
      at Immediate.Async.drainQueues [as _onImmediate] (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/async.js:17:14)
      at runCallback (timers.js:705:18)
      at tryOnImmediate (timers.js:676:5)
      at processImmediate (timers.js:658:5)
  </code></pre>
  
  <p>Please help anyone to solve this issue. </p>
  
  <p>Thanks in advance.</p>