title: NodeJS: TypeError: Cannot read property 'json' of undefined
body: <p>I'm creating a CronJob that calls an API and store its response in the database:</p>
  
  <pre><code>const CronJob = require("cron").CronJob;
  const btc_price_ticker = require("../../controllers/BtcExchange/Ticker");
  const currency = require("../../controllers/Currencies/CurrenciesController");
  
  module.exports = new CronJob("* * * * * *", async function() {
    const {
      ticker: { sell }
    } = await btc_price_ticker.getBtcPrice();
    currency
      .update({
        params: {
          id: "5cbdf078f5bcec257fcec792"
        },
        body: {
          exchange_rate: sell,
          lastUpdate: Date.now()
        }
      })
      .catch(error =&gt; console.log(error));
  });
  </code></pre>
  
  <p>It works fine, however I receive a <code>TypeError: Cannot read property 'json' of undefined</code></p>
  
  <p>I'm using the same function to update the database I use when updating by my API:</p>
  
  <pre><code>module.exports = {
    async update(req, res) {
      const currency = await Currency.findByIdAndUpdate(req.params.id, req.body, {
        new: true
      });
      return res.json(currency);
    }
  };
  </code></pre>
  
  <p>The <code>TypeError</code> happens in the <code>return res.json(currency)</code>, and it only happens when it's called by the CronJob. When I put new information by API, it doesn't show any error.</p>
  
  <p>I think it happens because when I call the function in <code>CronJob</code>, I just pass the <code>req</code> by parameter, but I don't know how to solve it. What am I supposed to do?</p>
  
  <p>Thanks in advance!</p>