I also had to face these problems recently. And I tried many solutions in stackoverflow, but in vain. Finally, I found the cause of the problem. I just want to share my experience with solving this problem. Usually people share the db configuration and the acl config, so they cause this problem.
The source of the problem is the built-in function node.js - async. If you tried to register the connection status using:
console.log(mongoose.connection.readyState);
In your db.js you will find 1 (connected); while in your acl.js it will be 2 (connection) unless you make acl in the appropriate block, which ensures that mongodb is already connected.
If you follow the most voted and most recent answer, your code might look like this:
var acl = require('acl'); var mongoose = require('../model/db'); mongoose.connection.on('connected', function(error){ if (error) throw error;
And then you can set your permissions and roles. But do not forget to make them in the block where the connection with the Mongod is already established. So your code should look like this:
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(); });
source share