angularjs - req.user data not displayed in the view -
i have express server passport-local stategy, passport-local , passport-local-mongoose installed.i want access current user model json read , write data it. headache object doesn't want display data inside view. it's loading no errors , cannot understand what's happening. please me!
here express config:
var express = require('express'), app = express(), morgan = require('morgan'), bodyparser = require('body-parser'), cookieparser = require('cookie-parser'), expresssession = require('express-session'), hash = require('bcrypt-nodejs'), mongoose = require('mongoose'), path = require('path'), passport = require('passport'), localstrategy = require('passport-local').strategy, port = process.env.port || 8080, user = require('./app/models/user'), routes = require('./app/routes/api'); mongoose.connect('mongodb://localhost:27017/dashboard'); app.use(express.static(__dirname + '/src')); app.use(morgan('dev')); app.use(bodyparser.urlencoded({extended: true})); app.use(bodyparser.json()); app.use(cookieparser()); app.use(require('express-session')({ secret: 'keyboard kat', resave: false, saveuninitialized: false })); app.use(passport.initialize()); app.use(passport.session()); passport.use(new localstrategy(user.authenticate())); passport.serializeuser(user.serializeuser()); passport.deserializeuser(user.deserializeuser()); app.use(function (req, res, next) { res.header('access-control-allow-origin', '*'); res.header('access-control-allow-methods', 'get', 'post'); res.header('access-control-allow-methods', 'x-requested-with, content- type, authorization'); next(); }); app.use('/user', routes); app.get('/', function (req, res) { res.sendfile(path.join(__dirname + '/src/views', 'index.html')); }); app.use(function (req, res, next) { var err = new error('not found :('); err.status = 404; next(err); }); app.use(function (err, req, res) { res.status(err.status || 500); res.end(json.stringify({ message: err.message, error: {} })); }); app.listen(port); console.log('magic happens on port ' + port);
and route use pull data:
router.get('/data', function (req, res) { res.send(req.user); });
Сhrome dev tools returns correct object:
angular service:
angular.module('main') .factory('menuservice', function ($http) { function getdata() { $http.get('/user/data') .success(function (data) { return data; }); } return { info: getdata } });
controller:
angular.module('main') .controller('menucontroller', function ($mdsidenav, menuservice) { var vm = this; vm.togglemenu = function () { $mdsidenav('menu').toggle(); }; vm.info = menuservice.info(); });
and finaly view:
<md-toolbar layout="row" ng-controller="menucontroller menu"> <div class="md-toolbar-tools"> <md-button class="md-icon-button md-primary" ng-click="menu.togglemenu()" aria-label="open menu"> <md-icon md-svg-icon="../../assets/img/hmg.svg"></md-icon> </md-button> <h1>dashboard</h1> <span flex></span> <h1>{{menu.info.name}}</h1> </div> </md-toolbar>
and nothing happens :( no view, no errors, absolutely nothing:
you need return promise when dealing async calls or use pub-sub pattern telling data arrived.
what happening getdata
function gets called, in background fetches data, not returned the caller of getdata
caller. why? because inside getdata
nothing returned. needs returned.
here solution returning promise.
update : sorry, forgot $http
returns resp object, has data. updated code according that. in service :
angular.module('main') .factory('menuservice', function ($http) { function getdata() { return $http.get('/user/data'); //no preprocessing returning result of http call } return { info: getdata } });
and in controller
angular.module('main') .controller('menucontroller', function ($mdsidenav, menuservice) { var vm = this; vm.togglemenu = function () { $mdsidenav('menu').toggle(); }; //todo called whenever menucontroller gets initiated //better put user action or run 1 time... menuservice.info().then(function(resp){ console.log(resp); //here response vm.info = resp.data; }, function(err){ //todo when no data arrived }); return vm; });
also .success
, .error
deprecated according docs
the $http legacy promise methods success , error have been deprecated. use standard method instead. if $httpprovider.uselegacypromiseextensions set false these methods throw $http/legacy error.
Comments
Post a Comment