Sunday, 7 May 2017

route.get() requires callback functions but got a object undefined at route.(anonymous function)

I checked and tried similar questions like this,but didn't fix the issue.I'm getting route.get() requires callback functions but got a object undefined at route.(anonymous function)

drug.server.policy.js

   'use strict';

    /**
     * Module dependencies
     */
    var acl = require('acl');

    // Using the memory backend
    acl = new acl(new acl.memoryBackend());

    /**
     * Invoke Drugs Permissions
     */
    exports.invokeRolesPolicies = function () {
      acl.allow([{
        roles: ['admin'],
        allows: [{
          resources: '/api/drugs',
          permissions: '*'
        }, {
          resources: '/api/drugs/:drugId',
          permissions: '*'
        }]
      }, {
        roles: ['user'],
        allows: [{
          resources: '/api/drugs',
          permissions: ['get', 'post']
        }, {
          resources: '/api/drugs/:drugId',
          permissions: ['get']
        }]
      }, {
        roles: ['guest'],
        allows: [{
          resources: '/api/drugs',
          permissions: ['get']
        }, {
          resources: '/api/drugs/:drugId',
          permissions: ['get']
        }]
      }]);
    };

    /**
     * Check If Drugs Policy Allows
     */

    exports.isAllowed = function (req, res, next) {
      var roles = (req.user) ? req.user.roles : ['guest'];

      // If an Drug is being processed and the current user created it then allow any manipulation
      if (req.drug && req.user && req.drug.user && req.drug.user.id === req.user.id) {
        return next();
      }

      // Check for user roles
      acl.areAnyRolesAllowed(roles, req.route.path, req.method.toLowerCase(), function (err, isAllowed) {
        if (err) {
          // An authorization error occurred
          return res.status(500).send('Unexpected authorization error');
        } else {
          if (isAllowed) {
            // Access granted! Invoke next middleware
            return next();
          } else {
            return res.status(403).json({
              message: 'User is not authorized'
            });
          }
        }
      });
    };

drug.server.routes.js

'use strict';

/**
 * Module dependencies
 */
var drugsPolicy = require('../policies/drugs.server.policy'),
  drugs = require('../controllers/drugs.server.controller');

module.exports = function(app) {
  // Prescriptions Routes

  app.route('/api/drugs').all(drugsPolicy.isAllowed)
    .get(drugs.list)
    .post(drugs.create);

  app.route('/api/drugs/:drugId').all(drugsPolicy.isAllowed)
    .get(drugs.read)
    .put(drugs.update)
    .delete(drugs.delete);

  // Finish by binding the DrugPrescription middleware
  app.param('drugId', drugs.drugByID);
};



via AnuradhaS

No comments:

Post a Comment