0

I have a button, when I click on it, my code will send a HTTP request through all selected items with angular.forEach in the first click I won't get any error or responses. from second click on the button and when my code try to send second part of $http requests I have an error.

error    Error: undefined is not an object (evaluating 'apiFactory.likeRequest')

this is my controller:

app.controller('dashboardCtrl',
  ['$rootScope', '$scope', 'store', '$state','apiFactory','loginFactory','$http',
 function($rootScope, $scope, store, $state, apiFactory, $http){

 apiFactory.likeRequest(item["media_id"], data)
   .then(function (response) {
                  console.log(response);
                 $scope.pressedIndexesObject.splice(index, 1);
            }, function (error) {
                  console.log(JSON.stringify(error));
                 $scope.pressedIndexesObject.splice(index, 1);
            });
});

and this ones is my request factory:

angular.module('instagrow')
    .factory('apiFactory', ['$http', function ($http) {
 var dataFactory = {};
      dataFactory.likeRequest = function (media_id,data) {
                return  $http.post('https://api.instagram.com/v1/media/'+ media_id + '/likes',  data, config);
            };

       return dataFactory;
    }]);

I tried to do it without factory or with closure as an argument but exception remained.

UPDATE: another type of request that tried was single instance from $http for each item like this :

    angular.forEach($scope.pressedIndexesObject, function(item) {
    var media = item
        console.log('im indexed' +JSON.stringify( $scope.pressedIndexesObject));

   $http.post('https://api.instagram.com/v1/media/'+ media["media_id"] + '/likes',  data)
   .then(function (response) {
                               console.log(response);

                 $scope.pressedIndexesObject.splice(index, 1);
            }, function (error) {
                                             console.log(JSON.stringify(error));

                 $scope.pressedIndexesObject.splice(index, 1);
            });

});
Pedram marandi
  • 1,474
  • 1
  • 19
  • 35
  • 3
    that's not nearly enough code for us, can you provide a bit more? the declarations themselves would help – Ven Apr 22 '16 at 14:49
  • @Ven updated, everything goes well because I used all of the functions through the project, I think, something wrong with my loop – Pedram marandi Apr 22 '16 at 14:55
  • You forgot the `dataFactory`/`apiFactory` (wat?) declaration. – Ven Apr 22 '16 at 14:59
  • dataFactory is an empty object that I store responses and return it to controller and other services, updated again @Ven – Pedram marandi Apr 22 '16 at 15:02
  • there is a typo, comma should be placed after `apiFactory` should `apiFactory $http` – Satpal Apr 22 '16 at 15:04
  • `dataFactory is an empty object that I store responses` Your not overwriting the object with those responses are you? If so you will be removing your `likeRequest` method. If you want to cache your API responses you should really use a separate object than the one that defines your factory itself. – ste2425 Apr 22 '16 at 15:07
  • I don't get what's your mean, I used ma apiFactory over 100 places in my project and it works correctly – Pedram marandi Apr 22 '16 at 15:07
  • @ste2425 in the last line I told ``I tried to do it without factory or with closure as an argument but exception remained.`` I tried to instantiate new $http object for each separately but exception was same – Pedram marandi Apr 22 '16 at 15:09
  • @Pedrammarandi Without seeing your implementation of that its impossible for me to know if your overwriting `dataFactory` or not, hence why i asked. – ste2425 Apr 22 '16 at 15:12
  • @ste2425 ok let me update my question with another ways that I've tried – Pedram marandi Apr 22 '16 at 15:15
  • It's a bad idea to change an array inside the forEach loop. Maybe first request is resolved before loop has finished... – Simon Schüpbach Apr 22 '16 at 16:43
  • A $http call returns a promise. Push all promises to an array then do $q.all() May suggest using [Angular $resource][1] http://stackoverflow.com/questions/15299850/angularjs-wait-for-multiple-resource-queries-to-complete?answertab=active#tab-top [1]: https://docs.angularjs.org/api/ngResource/service/$resource – jaecktec May 02 '16 at 05:49

0 Answers0