2016-12-23 3 views
0

У меня есть служба, которая загружает изображения из массива cloudinary.But, когда массив пуст, как я могу вернуть функцию без выполнения $ HTTP call.My код нижевозврат обещаний показывает ошибку в angularjs?

uploadPicture: function (files,title) { 

      if (!files) 
       promise = function(){} 
       return promise; 

      angular.forEach(files, function (file) { 
       if (file && !file.$error) { 

        promise = $upload.upload({ 
          url: "https://api.cloudinary.com/v1_1/" + cloudinary.config().cloud_name + "/upload", 
          data: { 
           upload_preset: cloudinary.config().upload_preset, 
           tags: 'myphotoalbum', 
           context: 'photo=' + title, 
           file: file 
          } 
         }).progress(function (e) {}) 
         .success(function (data, status, headers, config) { 
          return data; 
         }).error(function (data, status, headers, config) { 
          return data; 
         }); 
       } 
      }); 

      return promise; 
     } 

    } 

Если файлы равна нулю, то это показывает ошибку

delregservice.uploadPicture(...).then is not a function 

В моей controoler

$scope.Fn_SaveDeliveryAgent = function(){ 
       delregservice.uploadPicture($scope.filelist[0],$scope.title).then(function(d){ 
       $scope.delAgent.profile_pic = d; 
      }).then(function(){ 

      delregservice.uploadPicture($scope.filelist[1],$scope.title).then(function(d){ 
      $scope.delAgent.license_pic = d; 
      }).then(function(){ 

       delregservice.saveDeliveryAgent($scope.delAgent).then(function(d){ 
        console.log(d); 
      }); 
      }); 


      }); 
      } 

Как я могу решить эту проблему? Пожалуйста, помогите.Спасибо заранее и веселью X mas

+0

я сделать это, дней назад, и я включил обновление в соответствии с новым обещанием. Загрузка не является обещанием. Может быть, это –

ответ

0

Если файлы пусты, вы возвращаете пустую функцию, они не будут иметь функцию then.

простой Подход должен подождать, пока все обещания (с $q.all) и вернуть их:

uploadPicture: function (files,title) { 

     //if (!files) 
     // promise = function(){} 
     // return promise; 
     var promises = []; 
     angular.forEach(files, function (file) { 
      if (file && !file.$error) { 

       promises.push($upload.upload({ 
         url: "https://api.cloudinary.com/v1_1/" + cloudinary.config().cloud_name + "/upload", 
         data: { 
          upload_preset: cloudinary.config().upload_preset, 
          tags: 'myphotoalbum', 
          context: 'photo=' + title, 
          file: file 
         } 
        }) 
        // You wont need that here it will be passed with $q.all 
        //.progress(function (e) {}) 
        //.success(function (data, status, headers, config) { 
        // return data; 
        //}).error(function (data, status, headers, config) { 
        // return data; 
        //})); 
      } 
     }); 

     return $q.all(promises); 
    } 

} 

это гарантирует, что вы будете возвращаться каждый раз, когда действительное обещание

Смежные вопросы