0

I'm trying to access the length of an array returned from the promise object. However, I'm not getting any luck for it to work as it returns undefined when i check the array itself, and i try to access the specific element its says cannot read property

from the chrome console

f {$promise: Object, $resolved: false, $get: function, $save: function, $query: function…}
$promise: Object
$resolved: true
address: "1600 amphitheatre way , Mountain View, CA 94043"
address_1: "1600 amphitheatre way "
address_2: "Mountain View, CA 94043"
affiliation_attributes: Array[0]
award_attributes: Array[0]
certification_attributes: Array[0]
description: ""
description_read_more: false
education_attributes: Array[1]
id: 1
lat: "37.422003"
lng: "-122.083978"
name: "Gio Wong"
phone: "650-701-6411"
photos: Array[3]
practice_name: "Super Doctor"
rating: null
rating_url: null
salutation: "DDS"
services: Array[4]
specialty: "Pediatrician"
__proto__: f

I'm trying to access education,affiliation, award and certification attributes. The following code is how i print to console and how the promise object is get

$scope.provider = Provider.get($routeParams.name);
console.log($scope.provider)

how do i access $scope.provider.education_attributes.length? or even $scope.provider.education_attributes[0]? both returned cannot read property.

1

1 Answer 1

4

The provider function returns an Angular Promise object. It does not directly return your data. Instead you provide the Promise with a function to call once the Promise has resolved.

You should instead do:

Provider.get($routeParams.name).then(function(resolveData) {
  //access stuff on resolveData here
});

See "The Promise API" section of Angular's $q service.

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.