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)
6try following
angular.module('App') .controller('AdminCtrl',['$scope','$http','$auth', function ($scope, $http, $auth) { $http.get(') .then(function (res) { $scope.isAdmin = res.data; } }]);