Yi Wang
Yi Wang

Reputation: 1015

Node.js acl + mongoose, Cannot read property 'collection' of undefined

I am using node_acl (https://github.com/OptimalBits/node_acl), I initialized acl like this:

var mongoose = require('mongoose');
var acl = require('acl');

mongoose.connect(mainConfig.mongodbUrl, function(err, db){
    if(!err){
        acl = new acl(new acl.mongodbBackend(db, 'acl_'));
        acl.allow('guest', 'business', 'view');
    }
});

and got error:

   Unhandled rejection TypeError: Cannot read property 'collection' of undefined
        at /Users/ywang/fm/azure/node_modules/acl/lib/mongodb-backend.js:119:14
        at /Users/ywang/fm/azure/node_modules/async/lib/async.js:689:13
        at iterate (/Users/ywang/fm/azure/node_modules/async/lib/async.js:265:13)
        at async.forEachOfSeries.async.eachOfSeries (/Users/ywang/fm/azure/node_modules/async/lib/async.js:284:9)
        at _parallel (/Users/ywang/fm/azure/node_modules/async/lib/async.js:688:9)
        at Object.async.series (/Users/ywang/fm/azure/node_modules/async/lib/async.js:710:9)
        at Object.MongoDBBackend.end (/Users/ywang/fm/azure/node_modules/acl/lib/mongodb-backend.js:35:11)
        at Object.tryCatcher (/Users/ywang/fm/azure/node_modules/acl/node_modules/bluebird/js/main/util.js:26:23)
        at Object.ret [as endAsync] (eval at <anonymous> (/Users/ywang/fm/azure/node_modules/acl/node_modules/bluebird/js/main/promisify.js:163:12), <anonymous>:13:39)
        at Acl.allow (/Users/ywang/fm/azure/node_modules/acl/lib/acl.js:345:26)
        at /Users/ywang/fm/azure/server.js:18:13
        at open (/Users/ywang/fm/azure/node_modules/mongoose/lib/connection.js:478:17)
        at /Users/ywang/fm/azure/node_modules/mongoose/lib/connection.js:485:7
        at handleCallback (/Users/ywang/fm/azure/node_modules/mongoose/node_modules/mongodb/lib/utils.js:95:12)
        at _callback (/Users/ywang/fm/azure/node_modules/mongoose/node_modules/mongodb/lib/db.js:1278:5)
        at /Users/ywang/fm/azure/node_modules/mongoose/node_modules/mongodb/lib/db.js:1321:7
        at /Users/ywang/fm/azure/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:1015:5
        at /Users/ywang/fm/azure/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/auth/mongocr.js:115:17
        at /Users/ywang/fm/azure/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:756:13
        at Callbacks.emit (/Users/ywang/fm/azure/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:95:3)
        at null.messageHandler (/Users/ywang/fm/azure/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:243:23)
        at Socket.<anonymous> (/Users/ywang/fm/azure/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:262:22)

any idea where is wrong?

Thanks!!!!

Upvotes: 1

Views: 2319

Answers (1)

Kevin B
Kevin B

Reputation: 95057

The mongoose connect method does not give a second parameter. To get the db needed for the acl module, you need to get it directly from the mongoose instance.

mongoose.connect(mainConfig.mongodbUrl, function(err){
    if(!err){
        acl = new acl(new acl.mongodbBackend(mongoose.connection.db, 'acl_'));
        acl.allow('guest', 'business', 'view');
    }
});

http://mongoosejs.com/docs/

Upvotes: 4

Related Questions