javascript - How to get currently logged in user details from Promise object in jhipster based application -
i trying create 'order' object in front end , pushing database using rest services. pojo of 'order' looks below
@notnull @field("total") private bigdecimal total; @field("status") private string status; @embedded public user user;
now have 'principal' service providing information of logged in user.i have tried 'console.log(principal.identity())' returning result shown below. here 'user' data present inside '$$state' object.
not able find out how take 'user' data promise object , add 'order' object. have dirty method user data digging inside promise object shown below skeptical method.
what correct way data promise in scenario?
edit: jhipster based application. below "principle" service code
'identity: function (force) { var deferred = $q.defer(); if (force === true) { _identity = undefined; } // check , see if have retrieved identity data server. // if have, reuse resolving if (angular.isdefined(_identity)) { deferred.resolve(_identity); return deferred.promise; } // retrieve identity data server, update identity object, , resolve. account.get().$promise .then(function (account) { _identity = account.data; _authenticated = true; deferred.resolve(_identity); tracker.connect(); }) .catch(function() { _identity = null; _authenticated = false; deferred.resolve(_identity); }); return deferred.promise; }'
here jhipster generated method receive resource server using ngresource.
'angular.module('hotspiceapp') .factory('order', function ($resource, dateutils) { return $resource('api/orders/:id', {}, { 'query': { method: 'get', isarray: true}, 'get': { method: 'get', transformresponse: function (data) { data = angular.fromjson(data); return data; } }, 'update': { method:'put' } }); });'
the principal.identity()
function returns promise. first read promise.
then like:
principal.identity().then(function (user) { var data = { // other fields user: user }; // data inside inner function });
doing principal.identity().$$state.value
bad not because related on internal implementation of angular's promises won't work in cases. promises nature asynchronous , work because jhipster caches result of http request returns current user. if there no info current user @ time of new requst, principal.identity().$$state.value
undefined
because first need send http request server , after promise "resolved" (internally set value
variable , call function defined in then
method).
also should note, should not pass current user js code server. should take current user on server side (from session or something) after request arrived , set model if needed. never trust user code (code runs in user's browser). can inject value request , send server.
Comments
Post a Comment