I am trying to develop a user management feature for a website using the
MEAN.io stack. What I'm trying to do has worked in the past for other
models on the same site so I'm not sure what is going on. My issue is that
I am trying to get all the User models from the MongoDB database and pass
them to an AngularJS controller so that I can display their information on
a user management page. To that end, I added this function to the User
controller in the backend:
exports.readUsers = function(req, res) {
var decodedToken = req.decodeToken;
var User = mongoose.model('User');
var id = req.params.id;
existUser(decodedToken, function(err, user) {
if(err) {
return sendError(res, 'INVALID_TOKEN');
}
User.find({})
.select('username')
.exec(function(err, results) {
if(err) {
return sendError(res, err);
}
res.json({success: true, userList: results});
});
});}
This line to the routing code:
router.get('/users/all', Authorization.token, user.readUsers);
And this AngularJS controller for use with the frontend:
(function () {
"use strict";
var app = angular.module("GAP");
app.factory("UserEditFactory", function UserEditFactory($http, API_URL,
AuthTokenFactory, $q) {
"use strict";
return {
readUsers: readUsers
};
//Get all users in the DB
function readUsers() {
if(AuthTokenFactory.getToken()) {
return $http.get(API_URL + "/users/all");
}else {
return $q.reject({
data: "valid token required"
});
}
}
});
app.controller("userPageController", ["UserEditFactory", "$scope", "$http",
"$window", "API_URL",
function(UserEditFactory, $scope, $http, $window, API_URL) {
UserEditFactory.readUsers().then(function(data) {
console.log(data.data);
$scope.users = data.data.userList;
}, function(response) {
console.log(response);
});
}
]);})();
When I load the page that is supposed to display this information, no data is
displayed. I have determined that the AngularJS controller is calling the
second function which I understand is the one used to respond to an error.
Further investigation of the object returned by the $http.get call reveals no
data, and a status of -1. I'm not sure why this is happening, because I have
used this exact pattern of code to get and display data from other models in
the database on the same site. I can manually make HTTP calls to those working
functions from this controller, and everything works fine. I'm not sure where
to go from here or how to learn more about the issue. Can anyone offer insight?
Let me know if you need more information.
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.