i trying develop user management feature website using mean.io stack. i'm trying has worked in past other models on same site i'm not sure going on. issue trying user models mongodb database , pass them angularjs controller can display information on user management page. end, added function user controller in 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 routing code:
router.get('/users/all', authorization.token, user.readusers);
and angularjs controller use 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 users in 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 load page supposed display information, no data displayed. have determined angularjs controller calling second function understand 1 used respond error.
further investigation of object returned $http.get call reveals no data, , status of -1. i'm not sure why happening, because have used exact pattern of code , display data other models in database on same site. can manually make http calls working functions controller, , works fine. i'm not sure go here or how learn more issue. can offer insight? let me know if need more information.
edit: requested, here code authtokenfactory, app.factory object in common js file.
app.factory('authtokenfactory', function authtokenfactory($window) { 'use strict'; var store = $window.localstorage; var tokenkey = 'auth-token'; var userkey = "username"; return { gettoken: gettoken, settoken: settoken, setusername: setusername }; function gettoken() { return store.getitem(tokenkey); } function settoken(token) { if (token) { store.setitem(tokenkey, token); } else { store.removeitem(tokenkey); } } function setusername(username) { if (username) { store.setitem(userkey, username); } else { store.removeitem(userkey); } } });
Comments
Post a Comment