0
var promise = GetQuestions.quote.get();
console.log(promise);
promise.$promise.then(function(quote) {
  $scope.quotes = quote;
  console.log($scope.quotes);
  for( quote in $scope.quotes){
     console.log(quote.cite);
  }
});

factory

.factory('GetQuestions', ['$resource', function($resource){
   return {
      quote: $resource('**Resource Link**', {}, { get: { method: 'GET', isArray: true }})
   };
}])

Here is console.log(promise):

[$promise: Promise, $resolved: false]

Here is console.log($scope.quotes): enter image description here

And console.log(quote.cite) logs undefined

The Issue:

My issue is that I can't access the "cites", the promise is supposedly resolved however I can't seem to access the data correctly. I have tried doing promise.then() (instead of promise.$promise.then()), however I get an error telling me promise.then() is not a function. I am not sure why this is. Any help?

1 Answer 1

1

Instead of the for var in list syntax, try

$scope.quotes.forEach(function(quote) {
    console.log(quote.cite);
});

The for var in list should not be used to iterate over arrays. It's intended purpose is to iterate over object properties. When you use it to iterate over an array, you will often get weird behavior.

See this SO question.

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.