Per the current code here https://github.com/rschmukler/agenda/blob/a5b8a89df4d5bf4d7c667fa4a2559da8526da0df/lib/agenda.js#L318
Agenda.prototype.cancel = function(query, cb) {
this._collection.deleteMany( query, function( error, result ){
if (cb) {
cb( error, result && result.result ? result.result.n : undefined );
}
});
};
We should probably implement a safety check to ensure that this._collection is defined as well as this._collection.deleteMany. Unless there is a better option here?
Hi, a simple way to produce the error is to start with a database with no collection, you will get this error: TypeError: Cannot read property 'deleteMany' of undefined. I want to do this to ensure in my tests that there is no saved job before I run them. I can make a pull-request for it if you want.
@fabong PRs are always welcome!
I also get this error when running cancel at startup.
Cannot read property 'deleteMany' of undefined
the code is something like this (TS)
// tasks
import Logger from "../../../utils/Logger"
const logger = new Logger('Tasks')
import Project from '../models/Project'
import Agenda = require('agenda')
const mongoUri = process.env.MONGODB_URI || "mongodb://127.0.0.1:27017/test_database"
const TaskConfig = {
processInterval: '1 minutes',
scrapeInterval: '2 minutes'
}
const Tasks = {
async init() {
const agenda = new Agenda({
db: {
address: mongoUri,
collection: 'agendaTasks',
options: { useNewUrlParser: true }
},
processEvery: TaskConfig.processInterval
})
await agenda.cancel({}) // remove all jobs
// await agenda.purge() // remove not defined jobs
agenda.define('sayHello', job => {
logger.log('Hello!', job)
})
agenda.define('scrapeAll', async (job) => {
logger.log('run scrapeAll', job)
const opts = { start: 'now' }
const res = await Project.scrapeAll()
logger.log('scrapeAll result => ', res)
// Project.scrapeAll((result: any) => {
// logger.log('agenda scrapeAll done. result:', result)
// })
// done()
})
await agenda.start()
await agenda.every(TaskConfig.scrapeInterval, 'scrapeAll')
// await agenda.every('10 seconds', 'sayHello')
logger.log('agenda init done')
}
}
export default Tasks
// (async function () {
// // IIFE to give access to async/await
// await agenda.start();
// await agenda.every('3 minutes', 'delete old users');
// // Alternatively, you could also do:
// await agenda.every('*/3 * * * *', 'delete old users');
// })()
agenda:cancel attempting to cancel all Agenda jobs {} +0ms
unhandledRejection => TypeError: Cannot read property 'deleteMany' of undefined
at Agenda.module.exports [as cancel] (/Users/dc/dev/tix/recobot/stack/backend/node_modules/agenda/lib/agenda/cancel.js:14:27)
at Object.<anonymous> (/Users/dc/dev/tix/recobot/stack/backend/server.js:4135:26)
is new Agenda actually async until it has a connection?
well this seems to work
await agenda.start()
await agenda.cancel({}) // remove all jobs
await agenda.every(TaskConfig.scrapeInterval, 'scrapeAll', {})
Most helpful comment
Hi, a simple way to produce the error is to start with a database with no collection, you will get this error:
TypeError: Cannot read property 'deleteMany' of undefined. I want to do this to ensure in my tests that there is no saved job before I run them. I can make a pull-request for it if you want.