title: Nodejs Express multiple callback error
body: <p>I'm working on a homemade RBAC system with Nodejs Express, based on two levels:</p>
  
  <ul>
  <li>First, verify if the user has the right role to perform this action.</li>
  <li>Second, verify if the user has the right plan to perform this action.</li>
  </ul>
  
  <p>, I create a middleware like this:</p>
  
  <pre><code>exports.can = function (resource, action) {
      return function (request, response, next) {
          action = action || request.method;
          if (!request.user) {
              return next(new errors.UnauthorizedError());
          }
          request.user.can(request.user.role, request.user.currentPack, resource, action, function (can, error) {
              if (error) return next(error);
              if (!can) {
                  return next(new errors.UnauthorizedError());
              }
              return can;
          });
          return next();
      };
  };
  </code></pre>
  
  <p>I added to my User model this method:</p>
  
  <pre><code>const rbac = new RBAC(rbacJson);
  const pack = new PACK(packJson);
  schema.method('can', function (role, userPack, resource, action, next) {
      let can = false;
      action = action.toUpperCase();
      can = rbac.can(role, resource, action);
      if (can) {
          can = pack.can(userPack, resource, action, function (can) {
              return next(can);
          });
      }
      return next(can);
  });
  </code></pre>
  
  <p>In my method pack.can(...) I need to execute a mongoose query like this:</p>
  
  <pre><code>PACK.prototype.can = function (pack, resource, action, next) {
      let can = true;
      // some sequantial code
      Trader.count({/* some conditions */}, function (err, count) {
           if(count == 0) return next(true);
           return next(false);
      });
      return can;
  };
  </code></pre>
  
  <p>My problem is when the return of Mongoose query is next(false), I have this error:</p>
  
  <pre><code> Error: Can't set headers after they are sent.
   at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)
   at ServerResponse.header (/home/invoice/node_modules/express/lib/response.js:730:10)
   at ServerResponse.send (/home/invoice/node_modules/express/lib/response.js:170:12)
   at ServerResponse.json (/home/invoice/node_modules/express/lib/response.js:256:15)
   at ServerResponse.response.apiResponse (/home/invoice/server/config/middlewares/api.js:10:14)
   at /home/invoice/server/controllers/api/invoice/traders.js:130:21
   at /home/invoice/node_modules/mongoose/lib/model.js:3835:16
   at /home/invoice/node_modules/mongoose/lib/services/model/applyHooks.js:162:20
   at _combinedTickCallback (internal/process/next_tick.js:73:7)
   at process._tickDomainCallback (internal/process/next_tick.js:128:9)
  </code></pre>
  
  <p>After an investigation, I found that the error is maybe due to the double callback Call:</p>
  
  <ul>
  <li><code>can = pack.can(userPack, resource, action, function (can) {
              return next(can);
          });</code></li>
  <li><code>return next(new errors.UnauthorizedError());</code></li>
  </ul>
  
  <p>But I don't know how to solve this.
  I hope that I well explained my problem.</p>