A
A
Ayan Bai2016-05-11 19:04:56
Node.js
Ayan Bai, 2016-05-11 19:04:56

Why is req.acl sometimes not available?

I use mean.io for the project, during installation the system works stably, but acl in my module is sometimes not available. But the articles reference module is always available.
Here is mine where it happens:

var hasPermissions = function(req, res, next) {

  req.body.permissions = req.body.permissions || ['authenticated'];

  for (var i = 0; i < req.body.permissions.length; i++) {
    var permission = req.body.permissions[i];
    if(typeof(req.acl) === 'undefined'){
      console.log('req.acl: ', req.acl);
    }
    
    if (req.acl.user.allowed.indexOf(permission) === -1) {
      return res.status(401).send('User not allowed to assign ' + permission + ' permission.');
    }
  }

  next();
};

The function call happens here:
...
app.route('/api/' + name + e + 's')
      .get(controller.all)
      .post(auth.requiresLogin, hasPermissions, controller.create);
...

Error in console:
req.acl:  undefined
TypeError: Cannot read property 'user' of undefined
    at hasPermissions (/home/ubuntu/workspace/mean/packages/custom/bars/server/routes/bars.js:23:16)
    at Layer.handle [as handle_request] (/home/ubuntu/workspace/mean/node_modules/meanio/lib/core_modules/server/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/ubuntu/workspace/mean/node_modules/meanio/lib/core_modules/server/node_modules/express/lib/router/route.js:131:13)
    at /home/ubuntu/workspace/mean/packages/core/users/authorization.js:27:7
    at Query.<anonymous> (/home/ubuntu/workspace/mean/packages/core/users/authorization.js:12:9)
    at /home/ubuntu/workspace/mean/node_modules/kareem/index.js:177:19
    at /home/ubuntu/workspace/mean/node_modules/kareem/index.js:109:16
    at doNTCallback0 (node.js:407:9)
    at process._tickCallback (node.js:336:13)
POST /api/orders 500 - - 10.378 ms

Friends, tell me, what could be the problem?
Why is sometimes the user in rec.acl available and sometimes not?
This problem happens when writing data to the database. For example, when creating a new document...
Restarting the server helps to recover, sometimes just restarting is enough, and sometimes a full collection using the gulp command.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question