TypeError: Cannot read property ‘findOneAndUpdate’ of undefined – Node Agenda error – How to fix?
The Agenda
constructor is asynchronous, and hence you can’t trigger a job without the agenda being started. You can’t run or schedule a job before agenda has built a connection with MongoDB and is ready.
By ready, I mean when the event ready
is fired from agenda.
The following code would throw the error – TypeError: Cannot read property ‘findOneAndUpdate’ of undefined
agenda.on('ready', () => { agenda.start(); }) agenda.every('1 minute', 'JOB_ONE'); agenda.schedule('in 15 minutes', 'EMAIL_JOB')
Wrapping the every and schedule function calls inside the ready event will fix this error.
agenda.on('ready', () => { agenda.start(); agenda.every('1 minute', 'JOB_ONE'); agenda.schedule('in 15 minutes', 'EMAIL_JOB') })