0

I am storing data to $cacheFactory as such:

  $scope.loginCache = $cacheFactory.get('login') || $cacheFactory('login') ;
  $scope.loginCache.put("smsData",$scope.smsData) ;

And later am retrieving it with:

  $scope.loginCache = $cacheFactory.get('login') || $cacheFactory('login') ;
  $scope.smsData = $scope.loginCache.get('smsData') ;

However, I am unable to access the various elements of $scope.smsData. Out putting it to console.log does show all the values there but for some reason I am unable to access the data - what am I doing wrong?

console.log($scope.smsData) ;  // displays full object
console.log($scope.smsData.value) ;  // `undefined`
console.log($scope.smsData[0]) ;  // `undefined`
console.log($scope.smsData.value[0]) ; // `undefined`

console.log($scope.smsData) outputs:

Promise {$$state: {…}}
   $$state:
      status:1
      value:Array(2)
         0:{table: "smsSent", data: {…}}
         1:{apiStatus: true}

Ultimately, I am trying to retrieve: value[0].data

2
  • 2
    Looks like you are dealing with asynchronous callbacks. Try to resolve it like a Promise: $scope.smsData.then(function(res){console.log(res.value)}) Commented Aug 14, 2018 at 13:44
  • Yup, that was it. it was saving the promise versus just saving the results of the promise. Commented Aug 14, 2018 at 14:54

1 Answer 1

1

This is because your $scope.smsData is promise object and not a JSON object. What you can do is:

while storing:

$scope.loginCache = $cacheFactory.get('login') || $cacheFactory('login') ;
// first resolve the promise and then store in cache
$scope.smsData.then(function(response){
     $scope.loginCache.put("smsData",response) ;
});

so, when you will retrieve the data from this $cacheFactory, you will get your desired object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.