chilts / mongodb-queue

Message queues which uses MongoDB.
209 stars 91 forks source link

Cant connect #24

Closed rncortes1990 closed 5 years ago

rncortes1990 commented 6 years ago

Hi, Im trying to use this package on my mongodb database but the mongoqueue is not working, I checked database URI and its okay.

This is the code and the error

// Code
var mongodb = require('mongodb')
var mongoDbQueue = require('mongodb-queue')
var con = 'mongodb://localhost:27017/test'

mongodb.MongoClient.connect(con, function(err, db) {

    var queue = mongoDbQueue(db, 'my-queue')

})
// Error
E:\test nodemailer\node_modules\mongodb\lib\mongo_client.js:797
          throw err;
          ^

TypeError: mongoDbClient.collection is not a function
    at new Queue (E:\test nodemailer\node_modules\mongodb-queue\mongodb-queue.js:43:30)
    at module.exports (E:\test nodemailer\node_modules\mongodb-queue\mongodb-queue.js:29:12)
    at E:\test nodemailer\server.js:9:17
    at args.push (E:\test nodemailer\node_modules\mongodb\lib\utils.js:404:72)
    at E:\test nodemailer\node_modules\mongodb\lib\mongo_client.js:255:5
    at connectCallback (E:\test nodemailer\node_modules\mongodb\lib\mongo_client.js:933:5)
    at E:\test nodemailer\node_modules\mongodb\lib\mongo_client.js:794:11
    at _combinedTickCallback (internal/process/next_tick.js:95:7)
    at process._tickCallback (internal/process/next_tick.js:161:9)

thanks for your time.

mcnijman commented 6 years ago

It's probably because you are passing a MongoClient instance to mongodb-queue that is a newer version than the outdated one used by this module. The new versions do not automatically create a Db instance, so you should use client.db('name'), but also the method Db.collection without a callback used here in strict mode is deprecated in current versions: http://mongodb.github.io/node-mongodb-native/2.2/api/Db.html#collection http://mongodb.github.io/node-mongodb-native/3.0/api/Db.html#collection

coderofsalvation commented 6 years ago

versions should be fixed in https://github.com/chilts/mongodb-queue/pull/28

otherwise it's totally up to NPM to always install the latest mongodb package, which will prevent people from running this nice queue.

NOTE: this only works standalone. Chances are that you're already running some kind of mongodb version.

mcnijman commented 6 years ago

MongoDB 3.x introduced a breaking change: mongodb.MongoClient.connect doesn't return a mongodb.Db instance but a mongodb.MongoClient instead, so @rncortes1990 example should be changed into:

var mongodb = require('mongodb')
var mongoDbQueue = require('mongodb-queue')
var con = 'mongodb://localhost:27017/test'
mongodb.MongoClient.connect(con, function(err, client) {
    var db = client.db('test') // notice this
    var queue = mongoDbQueue(db, 'my-queue')
})

And it should work as expected. See for more: https://github.com/mongodb/node-mongodb-native/blob/master/CHANGES_3.0.0.md

chilts commented 5 years ago

v4.0.0 was released around 7 months ago and I think it should fix your problem. Please re-open if this isn't the case. Many thanks.