title: Can't set headers after they are sent. Nodejs/express with axios
body: <p>I have seen this error and more am just wanting to know how to work around it. I need to be able to output the data as a json object. Is there anyway to work around this. </p>
  
  <p>I am basically copying and pasting from a tutorial and not sure why Im getting it here except for the deep request calls maybe? </p>
  
  <p>Here is the error:</p>
  
  <pre><code>Error: Can't set headers after they are sent.
  [0]     at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)
  [0]     at ServerResponse.header (C:\Users\Amazo\Thrifa\moovr\node_modules\keystone\node_modules\express\lib\response.js:730:10)
  [0]     at ServerResponse.send (C:\Users\Amazo\Thrifa\moovr\node_modules\keystone\node_modules\express\lib\response.js:170:12)
  [0]     at ServerResponse.json (C:\Users\Amazo\Thrifa\moovr\node_modules\keystone\node_modules\express\lib\response.js:256:15)
  [0]     at C:/Users/Amazo/Thrifa/moovr/src/server/API/routes/offers.js:28:21
  [0]     at Promise.&lt;anonymous&gt; (C:/Users/Amazo/Thrifa/moovr/src/server/API/controllers/store/OfferController.js:25:7)
  [0]     at Promise.&lt;anonymous&gt; (C:\Users\Amazo\Thrifa\moovr\node_modules\keystone\node_modules\mongoose\node_modules\mpromise\lib\promise.js:177:8)
  [0]     at emitOne (events.js:77:13)
  [0]     at Promise.emit (events.js:169:7)
  [0]     at Promise.emit (C:\Users\Amazo\Thrifa\moovr\node_modules\keystone\node_modules\mongoose\node_modules\mpromise\lib\promise.js:84:38)
  [0]     at Promise.fulfill (C:\Users\Amazo\Thrifa\moovr\node_modules\keystone\node_modules\mongoose\node_modules\mpromise\lib\promise.js:97:20)
  [0]     at C:\Users\Amazo\Thrifa\moovr\node_modules\keystone\node_modules\mongoose\lib\query.js:1406:13
  [0]     at model.Document.init (C:\Users\Amazo\Thrifa\moovr\node_modules\keystone\node_modules\mongoose\lib\document.js:254:11)
  [0]     at completeOne (C:\Users\Amazo\Thrifa\moovr\node_modules\keystone\node_modules\mongoose\lib\query.js:1404:10)
  [0]     at Immediate.cb (C:\Users\Amazo\Thrifa\moovr\node_modules\keystone\node_modules\mongoose\lib\query.js:1158:11)
  [0]     at Immediate._onImmediate (C:\Users\Amazo\Thrifa\moovr\node_modules\keystone\node_modules\mongoose\node_modules\mquery\lib\utils.js:137:16)
  [0]     at processImmediate [as _immediateCallback] (timers.js:383:17)
  </code></pre>
  
  <p>Here is the code:</p>
  
  <pre><code>import OfferController from '../controllers/store/OfferController';
  import RequestManager from '../utils/RequestManager';
  import axios from 'axios';
  
  const offerRoutes = (app) =&gt; {
    app.get('/API/offers', (req, res, next) =&gt; {
      axios
        .get(
          'https://d2fzm6xoa70bg8.cloudfront.net/login?auth=e4031de36f45af2172fa8d0f054efcdd8d4dfd62',
        )
        .then((response) =&gt; {
          axios
            .get('https://d2fzm6xoa70bg8.cloudfront.net/offerfilter?', {
              headers: { Token: response.data.token },
            })
            .then((response) =&gt; {
              const { data } = response;
  
              data.map((item, i) =&gt; {
                OfferController.findOne({ offer: item.offer }, (err, result) =&gt; {
                  if (err) {
                    res.json({
                      message: err,
                    });
  
                    return;
                  }
                  res.json({
                    message: 'Offer already exist',
                    result,
                  });
                });
  
                const params = {
                  offer: item.offer,
                  cert: item.cert,
                  txid: item.txid,
                  expires_in: item.expires_in,
                  expires_on: item.expires_on,
                  expired: item.expired,
                  height: item.height,
                  time: item.time,
                  category: item.category,
                  title: item.title,
                  quantity: item.quantity,
                  currency: item.currency,
                  sysprice: item.sysprice,
                  price: item.price,
                  ismine: item.ismine,
                  commission: item.commission,
                  offerlink: item.offerlink,
                  offerlink_guid: item.offerlink_guid,
                  offerlink_seller: item.offerlink_seller,
                  private: item.private,
                  safesearch: item.safesearch,
                  safetylevel: item.safetylevel,
                  paymentoptions: item.paymentoptions,
                  paymentoptions_display: item.paymentoptions_display,
                  alias_peg: item.alias_peg,
                  description: item.description,
                  alias: item.description,
                  address: item.address,
                  alias_rating: item.alias_rating,
                  alias_rating_count: item.alias_rating_count,
                  alias_rating_display: item.alias_rating_display,
                  geolocation: item.geolocation,
                  offers_sold: item.offers_sold,
                };
                OfferController.create(params, (err, results) =&gt; {
                  if (err) {
                    console.log(err);
  
                    return;
                  }
  
                  res.write(results);
                });
              });
            })
            .catch((err) =&gt; {
              console.log('err 2nd request', err);
              res.json({
                confirmation: 'fail after 2nd request',
                message: err,
              });
            });
        })
        .catch((err) =&gt; {
          console.log('err 1st request');
          res.json({
            confirmation: 'fail before 2nd request',
            message: err,
          });
        });
    });
  };
  export default offerRoutes;
  </code></pre>