0

Here I am getting one issue while uploading multiple images on AngularJS. Here is my code and the error I'm getting.

$scope.saveFile = function(file) {
  return Upload.upload({
    url: CONFIG.apiUrl + '/fileupload',
    data: {
      fileUpload: file
    }
  }).success(function(data) {
    console.log("RSPPPPPPP", data.file._id);
    $scope.photoId = data.file._id;
    console.log("sdvfbghjm,kjhgfdsa", $scope.photoId);
  })
  return $scope.saveFile;
  //}       
};

$scope.Addprojects = function(prodetails) {
  if (prodetails.image1_id) {
    $scope.prodetails.file.push(prodetails.image1_id);
    //console.log("image1", prodetails.file);
  }
  if (prodetails.image2_id) {
    $scope.prodetails.file.push(prodetails.image2_id);
    //console.log("image2", prodetails.file);
  }
  //console.log(prodetails.file.length);
  if (prodetails.file.length == 2) {
    alert(prodetails.file.length);
    $scope.saveFile(prodetails.file[0]).then(function(res) {
      console.log("poooototot", res);
      $scope.saveFile(prodetails.file[1]).then(function(res) {
        $scope.Addprojectsimg(prodetails);
        console.log("", Addprojectsimg);
        alert('hai');
      });
    });
  } else if (prodetails.file.length == 1) {
    $scope.saveFile(prodetails.file[0]).then(function(res) {
      alert("ok");
      $scope.Addprojectsimg(prodetails);
    });
  } else {
    $scope.Addprojectsimg(prodetails);
  }
};

$scope.Addprojectsimg = function(prodetails){

                        console.log("projectadding",prodetails,$scope.photoId);
                        prodetails.image1_id= $scope.photoId;
                        console.log("SUB",prodetails);
                        $http.post(CONFIG.apiUrl+"/projectsubmit", prodetails).success(function(data, status) {
                        console.log("prorespons",data);
                        alert("images uploaded sucessfully");

                        })

                };

Error

Error: $scope.saveFile(...).then is not a function addprojectctrl/$scope.Addprojects@http://192.168.3.40:8081/2016/ANGULAR2016/ang-social/js/controllers.js:208:7 anonymous/fn@http://192.168.3.40:8081/2016/ANGULAR2016/ang-social/bower_components/angular/angular.js line 13365 > Function:2:332 ngEventHandler/http://192.168.3.40:8081/2016/ANGULAR2016/ang-social/bower_components/angular/angular.js:23613:17 $RootScopeProvider/this.$gethttp://192.168.3.40:8081/2016/ANGULAR2016/ang-social/bower_components/angular/angular.js:16052:16 $RootScopeProvider/this.$gethttp://192.168.3.40:8081/2016/ANGULAR2016/ang-social/bower_components/angular/angular.js:16152:20 ngEventHandler/<@http://192.168.3.40:8081/2016/ANGULAR2016/ang-social/bower_components/angular/angular.js:23618:17 n.event.dispatch@http://192.168.3.40:8081/2016/ANGULAR2016/ang-social/bower_components/jquery/dist/jquery.min.js:3:7467 n.event.add/r.handle@http://192.168.3.40:8081/2016/ANGULAR2016/ang-social/bower_components/jquery/dist/jquery.min.js:3:5583 return logFn.apply(console, args);

Please guide me to fix this issue. I think it small error only but i am new to AngularJS.

1 Answer 1

3

Your $scope.saveFile function isn't returning a promise, it should be like this:

$scope.saveFile = function(file) {
    return Upload.upload({
        url: CONFIG.apiUrl+'/fileupload',
        data: {fileUpload: file}
    }).success(function(data){
        console.log("RSPPPPPPP",data.file._id);
        $scope.photoId = data.file._id;
        console.log("sdvfbghjm,kjhgfdsa",$scope.photoId);

    });
};
Sign up to request clarification or add additional context in comments.

1 Comment

In AngularJS there's this difference between promises $q and $http, which uses other interface. More info here: peterbe.com/plog/promises-with-$http

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.