The problem here is that the Async resource is $ resource and how the console interacts with your data.
.query() is an asynchronous call, which means that it does not return a value immediately; instead, it allows the code to continue flowing. The following statement applies to console.log($scope.member); , which does not immediately open $scope.member , to see its properties until they are used or verified. The next call is console.log($scope.member.someProperty); which $scope.member will expand to find someProperty . Since the callback of your .query() has not yet been returned, it will be log undefined . By the time you go to check what is in the first console.log , the callback is complete, and so the object looks like it has all the properties all the time, although in fact it is not, at the time they access attempts were made.
An easy way to handle this is to wrap the calls in .then() to make sure you don't execute them until the promise from .query() returns. To bind .then() , you need to access the returned promise, i.e.
$scope.generateMemberToken = function () { $scope.member = Member.generateMemberToken('123456789', '789456123') .query().$promise.then(function(){ console.log($scope.member); console.log($scope.member.someProperty);
The .query() syntax allows an anonymous callback, which allows you to pass a function that should be executed when query() , rather than a chain of .then() statements. For instance:
$scope.generateMemberToken = function () { $scope.member = Member.generateMemberToken('123456789', '789456123') .query({}, function() { console.log($scope.member); console.log($scope.member.someProperty);
source share