Agenda: TypeError: this._mdb.collection is not a function

Created on 30 Mar 2017  路  14Comments  路  Source: agenda/agenda

While compiling the node application , I'm getting following error! Hope developers can help me out to sort this error. Working on Mongo reporting tool using agenda.

ypeError: this._mdb.collection is not a function
    at Agenda.db_init (/Worker/node_modules/agenda/lib/agenda.js:100:15)
    at Agenda.mongo (/Worker/node_modules/agenda/lib/agenda.js:58:8)
    at new module.exports (/Worker/node_modules/agenda/lib/agenda.js:46:10)
    at Object.<anonymous> (/Worker/app.js:30:14)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
support

Most helpful comment

For mongoose you need to use db.connection instead.

agenda = new Agenda({ 
  mongo: db.connection
})

All 14 comments

Even I'm getting the same error, did you find any solution @arpithparikh ?

Same issue when I supply an existing mongoClient instance (the variable 'db' here).

agenda = new Agenda({
    name: agendaName,
    mongo: db,
    db: { collection: agendaCollection },
    processEvery: processEvery
  })

Are you ensuring you don't try to initiate Agenda before you've really connected to MongoDB?

See example:
https://mongodb.github.io/node-mongodb-native/2.2/quick-start/quick-start/#connect-to-mongodb

same for Mongoose:
http://mongoosejs.com/docs/index.html

Did someone find a solution for that ?

I tried the following and it didn't work:

  • Letting agenda handle the connection.
  • Using the same mongoose connection as the one in my app and only initiating agenda once the connection is open.

In my case, it turned out that the variable being passed as 'db' was being mediated by another module and was not always resolving to a mongoClient instance. I cannot, however, speak to the OP's issue.

??? Anyone.. still getting this.

I'm getting this.databaseConnection.collection is not a function. Any idea ?!

For mongoose you need to use db.connection instead.

agenda = new Agenda({ 
  mongo: db.connection
})

I get the "this._mdb.collection is not a function" erorr if I try to instantiate Agenda by passing a mongoClient object to new Agenda({ mongo: mongoClient }):

```js
const mongoClient = await MongoClient.connect(mongoUrl, { useNewUrlParser: true });

config.agenda = new Agenda({
mongo: config.mongoClient,
});

The solution is to pass the database, not the client. If you only pass the mongoClient, Agenda would not know which database it should use.

```js
const mongoClient = await MongoClient.connect(mongoUrl, { useNewUrlParser: true });

config.agenda = new Agenda({
mongo: config.mongoClient.db(), // <-- note the .db()
});

@dandv that worked :)

Thanks @dandv !

also noticed this, think this is due to the changes in node-mongodb-native v3; see https://github.com/mongodb/node-mongodb-native/blob/master/CHANGES_3.0.0.md
MongoClient.connect works as expected but it returns the MongoClient instance instead of a database object.
So documentation should probably be updated to reflect these changes

this is due to the changes in node-mongodb-native v3
...
So documentation should probably be updated to reflect these changes

Good finding! Could someone do a pull request, please?

This worked for me:

const client = await MongoClient.connect("mongodb://localhost:27017");
const agenda = new Agenda().mongo(client.db("database_name"), "jobs");

As from version 3.0 of the mongodb nodejs native driver, you get an instance of the client object containing the database object instead

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sureshballa picture sureshballa  路  17Comments

leob picture leob  路  12Comments

niftylettuce picture niftylettuce  路  21Comments

Relecto picture Relecto  路  12Comments

arthursp picture arthursp  路  23Comments