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); };
drugs.server.controller.js
'use strict'; /** * Module dependencies. */ var path = require('path'), mongoose = require('mongoose'), Drug = mongoose.model('Drug'), errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller')), _ = require('lodash'); /** * Create a Drug */ exports.create = function(req, res) { var drug = new Drug(req.body); drug.user = req.user; drug.save(function(err) { if (err) { return res.status(400).send({ message: errorHandler.getErrorMessage(err) }); } else { res.jsonp(drug); } }); }; /** * Show the current Drug */ exports.read = function(req, res) { // convert mongoose document to JSON var drug = req.drug ? req.drug.toJSON() : {}; // Add a custom field to the Article, for determining if the current User is the "owner". // NOTE: This field is NOT persisted to the database, since it doesn't exist in the Article model. drug.isCurrentUserOwner = req.user && drug.user && drug.user._id.toString() === req.user._id.toString(); res.jsonp(drug); }; /** * Update a Drug */ exports.update = function(req, res) { var drug = req.drug; drug = _.extend(drug, req.body); drug.save(function(err) { if (err) { return res.status(400).send({ message: errorHandler.getErrorMessage(err) }); } else { res.jsonp(drug); } }); }; /** * Delete an Drug */ exports.delete = function(req, res) { var drug = req.drug; drug.remove(function(err) { if (err) { return res.status(400).send({ message: errorHandler.getErrorMessage(err) }); } else { res.jsonp(drug); } }); }; /** * List of Drug */ exports.list = function(req, res) { Drug.find().sort('-created').populate('user', 'displayName').exec(function(err, drugs) { if (err) { return res.status(400).send({ message: errorHandler.getErrorMessage(err) }); } else { res.jsonp(drugs); } }); }; /** * Drug middleware */ exports.drugByID = function(req, res, next, id) { if (!mongoose.Types.ObjectId.isValid(id)) { return res.status(400).send({ message: 'Drug is invalid' }); } Drug.findById(id).populate('user', 'displayName').exec(function (err, drug) { if (err) { return next(err); } else if (!drug) { return res.status(404).send({ message: 'No Drug with that identifier has been found' }); } req.drug = drug; next(); }); };
Stack Link:http://stackoverflow.com/questions/43830988/route-get-requires-callback-functions-but-got-a-object-undefined-at-route-ano
Comments
Post a Comment