pedroabreu / ion-gallery

Ionic gallery directive
MIT License
124 stars 61 forks source link

TypeError: Cannot read property 'length' of undefined. #43

Closed bekzhan7 closed 8 years ago

bekzhan7 commented 8 years ago

Hi, im getting this error when trying to fill items with images that i got from Rest request with jsonp method. Its popping this error then loading of my images are done. How we can fix this issue?

`var items = [];
    Gallery.photos(albumId).success(function(data){
      if(data.response && data.response.count>0){
        var photos = data.response.items;
        for(var i = 0;i<photos.length;i++){
          items[i] = {
            src: photos[i].sizes[photos[i].sizes.length-1].src,
            sub: photos[i].text,
            thumb: photos[i].sizes[0].src,
          }
        }
        console.log(items);
        $scope.items = items;
      }
)}`

And services.js return promise of jsonp method.

pedroabreu commented 8 years ago

Can you share the solution for this ? Just in case someone runs into the same issue

bekzhan7 commented 8 years ago

Yes, my problem was that items scope variable was empty when view loads. It happened because of asynchronous jsonp request. So, instead of filling local variable items and then assigning it to a scope variable, i filled scope.items directly, like

$scope.items = [];
    Gallery.photos(albumId).success(function(data){
      if(data.response && data.response.count>0){
        var photos = data.response.items;
        for(var i = 0;i<photos.length;i++){
          $scope.items[i] = {
            src: photos[i].sizes[photos[i].sizes.length-1].src,
            sub: photos[i].text,
            thumb: photos[i].sizes[0].src,
          }
        }
      }
)}