Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 运行';节点acl';快车上有猫鼬_Node.js_Mongodb_Express_Mongoose_Acl - Fatal编程技术网

Node.js 运行';节点acl';快车上有猫鼬

Node.js 运行';节点acl';快车上有猫鼬,node.js,mongodb,express,mongoose,acl,Node.js,Mongodb,Express,Mongoose,Acl,我正在ExpressJS上构建一个应用程序(类似于博客)。我使用mongoose与MongoDB合作 当我不得不在各种ACL模块之间进行选择时,我决定使用它 . 让我困惑的是,它使用的是mongodb模块,而不是mongoose 根据网站上的文档,它必须以这种方式使用: // Or Using the mongodb backend acl = new acl(new acl.mongodbBackend(dbInstance, prefix)); 如果我使用mongoose,db的实例会是什

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

当我不得不在各种ACL模块之间进行选择时,我决定使用它 . 让我困惑的是,它使用的是mongodb模块,而不是mongoose

根据网站上的文档,它必须以这种方式使用:

// Or Using the mongodb backend
acl = new acl(new acl.mongodbBackend(dbInstance, prefix));
如果我使用mongoose,db的实例会是什么

我用的是:
Account=mongoose.model('Account',新模式({…}))

在我的脑海里,我想你在寻找这个:

示例(未测试):


(当然,这是假设您已经在别处用Mongoose.connect()初始化了Mongoose。)

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

在创建初始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);
});

我最近也碰巧遇到了这个问题。我在stackoverflow上尝试了很多解决方案,但都没有成功。最后我找到了问题的原因。我只是想分享一下我解决这个问题的经验。通常,人们会将db配置和acl配置分开,从而导致此问题

问题的根源是node.js的本机特性--async。如果您尝试使用以下方式记录连接状态:

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

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

});
然后您可能需要设置权限和角色。但是请记住,在已经建立到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_'));

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

});

我试过这种方法,但没有成功。ACL集合没有出现在我的数据库上,并且系统不工作。知道这有点旧,但尝试执行类似的操作,嗯,不,我正尝试执行您正试图执行的操作,您是否曾经让它工作过?如何在此文件外使用ACL实例?