Utilizing the MEAN stack in JavaScript for my single page application has been seamless.
A crucial component of my architecture involves an Angular factory that communicates with my API.
app.factory('authorizing', function($resource){
return $resource(
'/api/authorizing/:username',
{'username':'@username'},
// fetching data for a single user along with their roles
{'singleUser' : {
method:'GET'
,isArray: false
}}
);
});
The integration of this factory within my controller is key as it allows me to dynamically update the webpage based on API responses. The expected outcome is either a true or false value, and I prefer keeping authorization logic server-side.
app.controller('usrPageController', function ($scope, usrServiceById, $route, authorizing, $rootScope) {
$scope.updateUser = function (data, field){
authorizing.singleUser({'username': $rootScope.current_user}, function(response){
if (response.data == 'true'){
usrServiceById.update({'username':username},{field:data, 'fieldName':field});
}
});
};
});
Upon querying the database, results are sent back using res.send.
.get(function (req, res) {
RoleUser.find({ 'aclUserName': req.params.username }, function (err, aclUser) {
if (err) { return res.sendStatus(500) };
if (aclUser == null) { return res.sendStatus(401) };
for (var i = 0; i < aclUser.length; i++) {
if (aclUser[i].aclUserResource == req.params.username + '_PROFILE') {
return res.send('true');
};
};
return res.send('false');
});
});
While there are no errors present in the code execution, upon reaching the controller, the returned object appears empty after res.send. Attempting various data types for the response has proved futile. Assistance would be greatly appreciated as other res.send calls within my API work seamlessly when directly extracting data from the database through callbacks. This specific scenario marks the only instance where returning something aside from a successful callback variable proves challenging.