UnhandledPromiseRejectionWarning when using agenda

1k Views Asked by At

I wrote some code by agenda package and I've got UnhandledPromiseRejectionWarning when I run my project.

My code here:

agenda.define('transferDBField', (job, done) => {
    if (this.tPrice) {
        this.prices.push(this.tPrice);
        done();
    }
    done();
});
agenda.every('1 days', 'transferDBField');

The warning is this:

(node:1992) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'findOneAndUpdate' of undefined
    at Agenda.module.exports [as saveJob] (C:\Users\Sayyid Ali Sajjadi D\Desktop\gheymat\node_modules\agenda\lib\agenda\save-job.js:105:45)
    at Job.module.exports [as save] (C:\Users\Sayyid Ali Sajjadi D\Desktop\gheymat\node_modules\agenda\lib\job\save.js:13:22)
    at createJob (C:\Users\Sayyid Ali Sajjadi D\Desktop\gheymat\node_modules\agenda\lib\agenda\every.js:29:15)
    at Agenda.module.exports [as every] (C:\Users\Sayyid Ali Sajjadi D\Desktop\gheymat\node_modules\agenda\lib\agenda\every.js:56:24)
    at Object.<anonymous> (C:\Users\Sayyid Ali Sajjadi D\Desktop\gheymat\app\models\product.js:32:8)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (C:\Users\Sayyid Ali Sajjadi D\Desktop\gheymat\app\routes\v1\home.js:3:13)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (C:\Users\Sayyid Ali Sajjadi D\Desktop\gheymat\app\routes\index.js:7:31)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
(node:1992) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:1992) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Its sample code is this:

agenda.define('printAnalyticsReport', (job, done) => {
  User.doSomethingReallyIntensive((err, users) => {
    processUserData();
    console.log('I print a report!');
    done();
  });
});

agenda.every('15 minutes', 'printAnalyticsReport');

I code like the sample code but I don't know why do I get warning!

2

There are 2 best solutions below

0
On BEST ANSWER

please start agenda before use 'every' like this :

(async function() { // IIFE to give access to async/await
await agenda.start();

await agenda.every('15 minutes', 'printAnalyticsReport');

// Alternatively, you could also do:
// await agenda.every('*/15 * * * *', 'printAnalyticsReport');
})(); 
0
On

It works pretty well for me. Refer click here for more info.

const Agenda = require('agenda')

agenda.define('activer', async () => {
  await agenda.start()
  console.log(activer)
})
agenda.on('ready', () => {
  agenda.start()
  agenda.every('1 minute', 'activer')
})