返回承诺在angularjs中显示错误?

返回承诺在angularjs中显示错误?,angularjs,Angularjs,我有一个将图像从数组上传到cloudinary的服务。但当数组为空时,我如何在不执行$http调用的情况下返回函数。下面是我的代码 uploadPicture: function (files,title) { if (!files) promise = function(){} return promise; angular.forEach(files, function (fi

我有一个将图像从数组上传到cloudinary的服务。但当数组为空时,我如何在不执行$http调用的情况下返回函数。下面是我的代码

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
在我的控制器中

$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);
             });
             });


            });
            }

我怎样才能解决这个问题?请帮助。提前谢谢,如果文件是空的,您返回的是空函数,它们就没有then函数

一个简单的方法是等待所有承诺并将其返还:

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);
    }

}

这确保了每次我在几天前做出有效承诺,并且我在新承诺下解决了升级问题时,您都会返回。上传不是承诺。可能是这个