title: Agenda Cannot read property insertOne of undefined
body: <p>I am trying to create and schedule jobs with agenda. So i split it into two files, one file the <code>consumer</code> creates jobs and the other file the <code>producer</code> schedules jobs.</p>
  <p>this is the consumer file</p>
  <pre><code>const Agenda = require(&quot;agenda&quot;);
  async function run() {
      try {
          const agenda = new Agenda({
              db: {
                  address: process.env.MONGODB_URL,
                  collection: 'agendaJobs',
                  options: {
                      useUnifiedTopology: true
                  }
              }
          });
  
          agenda.define('ticket creation', async (job, done) =&gt; {
              const { time } = job.attrs.data;
              console.log(time);
              done();
          });
  
          agenda.on(&quot;ready&quot;, function () {
              agenda.start();
              console.log('Agenda Started');
          });
          //await agenda.start();
  
      } catch (error) {
          console.error(error);
          process.exit(-1);
      }
  
  }
  
  module.exports = {
      run: run
  };
  </code></pre>
  <p>and the producers file is</p>
  <pre><code>//import { default as Agenda } from 'agenda';
  const Agenda = require(&quot;agenda&quot;);
  
  const jobs = async (job) =&gt; {
      try {
          const agenda = new Agenda({
              db: {
                  address: process.env.MONGODB_URL,
                  collection: 'agendaJobs',
                  options: {
                      useUnifiedTopology: true
                  }
              }
          });
  
          const created = await agenda.schedule(job.time, 'ticket creation', {
              time: job.time
          });
          
          return created;
          
      } catch (error) {
          console.error(error);
          process.exit(-1);
      }
  };
  
  module.exports = jobs;
  </code></pre>
  <p>and this is my controller where i use it</p>
  <pre><code>const jobs = require('./producer');
  const { run } = require('./consumer');
  run();
  
  app.post('/create/job', async (req, res) =&gt; {
      try {
          const { time } = req.body;
          await jobs({
              time: time
          });
          return res.status(200).send({
              success: true,
              message: 'Job created'
          });
      } catch (error) {
          return res.status(error.status).send(error);
      }
  });
  </code></pre>
  <p>is there anywhere i am wrong, i know this connects to the db</p>
  <p>an example of the body sent to the controller</p>
  <pre><code>{
      &quot;time&quot;: &quot;3 minutes&quot;
  }
  </code></pre>
  <p>i have used the node debugger to trace the error, it always fails at a line that looks like this, but i don't know what is causing it</p>
  <pre><code>yield schedule(this).save()
  </code></pre>
  <p>then shows an error in the terminal saying</p>
  <pre><code>TypeError: Cannot read property 'insertOne' of undefined
  </code></pre>
  <p>** The props property of <code>this</code> is always undefined</p>