在ExpressJS上使用mongoose运行“node_acl”

我在ExpressJS上构build了一个应用程序(类似于博客)。 我使用mongoose与MongoDB合作。

当我不得不在不同的ACL模块之间进行select时,我决定采用node_acl 。 令我困惑的是它使用的是mongodb模块,而不是mongoose。

根据ACL GitHub上的文档,必须这样使用:

// Or Using the mongodb backend acl = new acl(new acl.mongodbBackend(dbInstance, prefix)); 

如果我正在使用mongoose,那将会是一个db的实例?

我使用类似于:Account = mongoose.model('Account',new Schema({…}));

关于我的头顶,我想你正在寻找这个:

http://mongoosejs.com/docs/api.html#connection_Connection-db

示例(未testing):

 var mongoose = require('mongoose'), acl = require('acl'); acl = new acl(new acl.mongodbBackend(mongoose.connection.db, 'acl_')); 

(这当然假设你已经用mongoose.connect()在其他地方初始化了Mongoose)

我最近也遇到了这个问题。 我在stackoverflow上尝试了很多很多的解决scheme,但徒劳无功。 最后我find了问题的原因。 只是想分享我的经验来解决这个问题。 通常人们将db config和acl config分开,从而导致这个问题。

问题的根源是node.js的本地特性 – asynchronous。 如果您尝试使用以下方式logging连接状态:

 console.log(mongoose.connection.readyState); 

你会发现在你的db.js,它是1(连接); 而在你的acl.js中,如果你没有在确保mongodb已经连接的适当的块中做了acl,那么它将是2(连接)。

如果你按照最投票和最新的答案,你的代码可能是这样的:

 var acl = require('acl'); var mongoose = require('../model/db'); mongoose.connection.on('connected', function(error){ if (error) throw error; //you must set up the db when mongoose is connected or your will not be able to write any document into it acl = new acl(new acl.mongodbBackend(mongoose.connection.db, 'acl_')); }); 

然后你可能想要设置你的权限和angular色。 但要记住在已经build立了与mongodb的连接的块中。 所以最后你的代码应该是这样的:

 var acl = require('acl'); var mongoose = require('../model/db'); mongoose.connection.on('connected', function(error){ if (error) throw error; //you must set up the db when mongoose is connected or your will not be able to write any document into it acl = new acl(new acl.mongodbBackend(mongoose.connection.db, 'acl_')); //Do acl.allow('role', ['resources'], ['actions'] here initACLPermissions(); //Do acl.addUserRolss('id', 'role') here initACLRoles(); }); 

好,回答bncc关于不工作的评论。

在我创build初始acl数据库的设置脚本中,我必须确保在尝试写入之前,mongoose已经打开了连接。 通常这不是和应用程序的问题,但在这种情况下,将所有acl命令包装在db.connection.success

 // Lets open database var cfgDB = require('../config/database') var acl = require('acl') var mongoose = require('mongoose') var dbconnection = mongoose.connect(cfgDB.mongoUrl, function(err) { if(err) console.log('MongoDb: Connection error: ' + err); }) mongoose.connection.on('open', function (ref) { console.log('Connected to mongo server.'); //var dbconnection = mongoose.connect('mongodb://localhost/acl-test', {}); console.log("Lets do this to " + dbconnection.connection.db) acl = new acl(new acl.mongodbBackend(dbconnection.connection.db, "acl_")); // initialize acl system storing data in the redis backend //acl = new acl(new acl.mongodbBackend(dbconnection, "acl_")); /* now assign permissions to roles */ // allow guests to view posts acl.allow("guest", "/index", "view"); // allow registered users to view and create posts //acl.allow("registered users", "post", ["view", "create"]); // allow administrators to perform any action on posts // acl.allow("administrator", "/", "*"); }); mongoose.connection.on('error', function (err) { console.log('Could not connect to mongo server!'); console.log(err); });