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

});
12
  • 3
    that's not nearly enough code for us, can you provide a bit more? the declarations themselves would help Commented Apr 22, 2016 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 Commented Apr 22, 2016 at 14:55
  • You forgot the dataFactory/apiFactory (wat?) declaration. Commented Apr 22, 2016 at 14:59
  • dataFactory is an empty object that I store responses and return it to controller and other services, updated again @Ven Commented Apr 22, 2016 at 15:02
  • there is a typo, comma should be placed after apiFactory should apiFactory $http Commented Apr 22, 2016 at 15:04

0

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.