I am currently working on creating an 'Order' object in the frontend and attempting to push it into the database using REST services. The POJO of the 'Order' looks like this:
@NotNull
@Field("total")
private BigDecimal total;
@Field("status")
private String status;
@Embedded
public User user;
At the moment, I have a 'Principal' service that provides information about the currently logged-in user. When I use 'console.log(Principal.identity())', it returns the following result with the 'User' data inside the '$$state' Object.
https://i.sstatic.net/VS0sY.png
I am struggling to figure out how to extract the 'user' data from the promise object and add it to the 'Order' object. I have come up with a method to retrieve user data by delving into the Promise object as shown below, but I'm unsure about its effectiveness.
https://i.sstatic.net/92H8p.png
Is there a more optimal way to extract data from the Promise in this particular scenario?
EDIT: This application is based on JHipster. Below is the code for the "Principal" service:
'identity: function (force) {
var deferred = $q.defer();
if (force === true) {
_identity = undefined;
}
// check and see if we have retrieved the identity data from the server.
// if we have, reuse it by immediately resolving
if (angular.isDefined(_identity)) {
deferred.resolve(_identity);
return deferred.promise;
}
// retrieve the identity data from the server, update the identity object, and then 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 is the method generated by JHipster to receive resources from the 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' }
});
});'