OptimalBits / node_acl

Access control lists for node applications
2.62k stars 369 forks source link

ACL Error 'Callback was already called' #142

Open muratatak77 opened 9 years ago

muratatak77 commented 9 years ago

When it works app, created acl collections in MongoDb. But , Error occured acl.allow() method. I get errors.

  var express = require('express');
  var path = require('path');
  var favicon = require('serve-favicon');
  var logger = require('morgan');
  var cookieParser = require('cookie-parser');
  var bodyParser = require('body-parser');

  var routes = require('./routes/index');
  var users = require('./routes/users');

  var mongodb = require('mongodb');
  var node_acl = require('acl');
  var acl = "";

  var app = express();

  mongodb.connect('mongodb://127.0.0.1:27017/acl', _mongo_connected);

  function _mongo_connected(error, db) {
    var mongoBackend = new node_acl.mongodbBackend(db /*, {String} prefix   */ );
    // Create a new access control list by providing the mongo backend
    / /  Also inject a simple logger to provide meaningful output
     acl = new node_acl(mongoBackend);

     // Defining roles and routes
     set_roles();
     }

  function set_roles() {

    // Define roles, resources and permissions
    acl.allow([{
     roles: 'admin',
     allows: [{
       resources: '/secret',
       permissions: 'create'
     }, {
       resources: '/topsecret',
       permissions: '*'
     }]
     }, {
     roles: 'user',
       allows: [{
       resources: '/secret',
       permissions: 'get'
     }]
    }, {
     roles: 'guest',
     allows: []
    }]);

   Code continue ...
   ..
   .
   ..

   }

ERRORS :

 node_modules/mongodb/lib/mongo_client.js:439       
 process.nextTick(function() { throw err; });
                                ^
   Error: Callback was already called.
   at node_modules/acl/node_modules/async/lib/async.js:43:36
   at node_modules/acl/node_modules/async/lib/async.js:694:17
   at node_modules/acl/node_modules/async/lib/async.js:173:37
   at node_modules/acl/lib/mongodb-backend.js:134:18
   at handleCallback (node_modules/mongodb/lib/utils.js:95:12)
   at node_modules/mongodb/lib/db.js:845:28
   at handleCallback (node_modules/mongodb/lib/utils.js:95:12)
   at node_modules/mongodb/lib/db.js:1538:5
   at handleCallback (node_modules/mongodb/lib/utils.js:95:12)
   at node_modules/mongodb/lib/db.js:273:5
   at node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:756:13
gitawego commented 9 years ago

I've exactly the same problem. I think in mongodb-backend.js, the cb is called twice:

// update document
        collection.update(updateParams, {$set: doc}, {safe: true, upsert: true}, function (err) {
          if (err instanceof Error) return cb(err);
          cb(undefined);
        });
        // Create index
        collection.ensureIndex({_bucketname: 1, key: 1}, function (err) {
          if (err instanceof Error) {
            return cb(err);
          } else {
            cb(undefined);
          }
        });

this should become:

// Create index
        collection.ensureIndex({_bucketname: 1, key: 1}, function (err) {
          if (err instanceof Error) {
            return cb(err);
          } else {
            // update document
            collection.update(updateParams, {$set: doc}, {safe: true, upsert: true}, function (err) {
              if (err instanceof Error) return cb(err);
              cb(undefined);
            });
          }
        });
gitawego commented 9 years ago

please merge the pull request 140...this bug is critical... :(

muratatak77 commented 9 years ago

Yes. it works. Thanks.

lionng429 commented 9 years ago

@gitawego appreciate, but not feeling good with the console.log on Line: 36

gitawego commented 9 years ago

@lionng429 this is already fixed in master. see pull request 140, don't need to merge mine.

shivendrasoni commented 8 years ago

Shouldn't this issue be closed now ?