Node.js acl + mongoose,无法读取属性'集合'未定义的

时间:2015-09-23 19:30:16

标签: node.js mongoose acl

我正在使用node_acl(https://github.com/OptimalBits/node_acl), 我初次化了这样的acl:

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');
    }
});

并收到错误:

   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)

任何想法哪里出错?

感谢!!!!

1 个答案:

答案 0 :(得分:4)

mongoose connect方法不提供第二个参数。要获取acl模块所需的数据库,您需要直接从mongoose实例获取它。

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/