I'm trying to make an isAdmin() function that will check if the current user has "isAdmin: true" in the mongodb.

server.js

app.get('/api/isadmin', function (req, res) { User.findById(req.user, function (err, user) { if (req.user.isAdmin == true) { res.send(user); } else { return res.status(400).send({ message: 'User is not Admin' }); } }); }); 

AdminCtrl.js

angular.module('App') .controller('AdminCtrl', function ($scope, $http, $auth) { $http.get(') .then(function (res) { $scope.isAdmin = res.data; } }); 

when I access the page /admin it throws "cannot read property 'isAdmin' of null" in the if inside app.get. Why is this occuring, and what is the optimal way for me to make this isAdmin function?

2 Answers

You are not using the variable sent back to you, but still the req.

app.get('/api/isadmin', function (req, res) { User.findById(req.user, function (err, user) { if (user.isAdmin == true) { res.send(user); } else { return res.status(400).send({ message: 'User is not Admin' }); } }); }); 

Maybe this should work (assuming you are returned if the user isAdmin)

6

try following

angular.module('App') .controller('AdminCtrl',['$scope','$http','$auth', function ($scope, $http, $auth) { $http.get(') .then(function (res) { $scope.isAdmin = res.data; } }]); 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.