2016-12-23 15 views
0

私は、アレイは、私は返信約束は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; 
     } 

    } 

以下の実行$ HTTP call.Myコードファイルがnullの場合、それはエラー

を示しせずに機能を返すことができますどのように空になったときcloudinary.Butに、アレイからの画像をアップロードサービスを持っています私controolerで
delregservice.uploadPicture(...).then is not a function 

$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マス

+0

を返すことを保証し、I新しい約束の下でupdloadをenvolvedしました。アップロードは約束ではありません。これかもしれない –

答えて

0

ファイルが空の場合、空の関数を返していますが、それらの関数はありません。

シンプルaproachは($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); 
    } 

} 

これはあなたが日前、私はそれを行うたびに、有効な約束

関連する問題