1

I'm writing a script to populate my app database with Dummy Data as I am getting tired of manually adding users/friends/etc, I'm doing this by nesting AngularJS $http requests that speak to my app backend API/Rest service...

So far all is working well but now I need to loop async calls, like so... please note that this is the 3rd nested $http call

// 3. Create Auto Friend For that User
for (var i = 1; i < 6; i++) { // 6 is just a given number, it could be 1 or 100....
   ($http({method: 'POST', url: '/path/to/rest/friend', data: {"name":"Auto Friend " + i}})
      .then(function (response) {
         console.log("friend created");
         console.log(response);

      }, function () {
         console.log('Whoops...');
      }))(i); // THIS IS LINE 69
}

I'm currently getting the following error...

TypeError: object is not a function
    at http://localhost:9000/assets/js/src/app/auth/controllers/AuthCtrl.js:69:40
    at wrappedCallback (http://localhost:9000/assets/js/vendor/bower/angular/angular.js:10549:81)
    at http://localhost:9000/assets/js/vendor/bower/angular/angular.js:10635:26
    at Scope.$eval (http://localhost:9000/assets/js/vendor/bower/angular/angular.js:11528:28)
    at Scope.$digest (http://localhost:9000/assets/js/vendor/bower/angular/angular.js:11373:31)
    at Scope.$delegate.__proto__.$digest (<anonymous>:844:31)
    at Scope.$apply (http://localhost:9000/assets/js/vendor/bower/angular/angular.js:11634:24)
    at Scope.$delegate.__proto__.$apply (<anonymous>:855:30)
    at done (http://localhost:9000/assets/js/vendor/bower/angular/angular.js:7635:45)
    at completeRequest (http://localhost:9000/assets/js/vendor/bower/angular/angular.js:7801:7)

I've made some slight amends to my code but nothing seems to work, I assume my approach is wrong rather than the code (but I could be wrong there). Has anyone got an idea or recomendation on how i can overcome this issue?

3
  • 1
    What's the purpose of wrapping the $http within ()? Commented Nov 19, 2013 at 10:14
  • To create a closure - if you don't, the value of i may have changed before the http get is actually performed. Commented Nov 19, 2013 at 10:17
  • Hi CodeHater, I was trying to freeze the value of i Commented Nov 19, 2013 at 10:17

1 Answer 1

3

I think I may have answered this myself, I didn't wrap the code in a function...

for (var i = 1; i < 6; i++) { // 6 is just a given number, it could be 1 or 100....
   (function(i){
     http({method: 'POST', url: '/path/to/rest/friend', data: {"name":"Auto Friend " + i}})
      .then(function (response) {
         console.log("friend created");
         console.log(response);

      }, function () {
         console.log('Whoops...');
         });
      })(i);
}

Next time I will open my eyes before posting, thanks for the comments...

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

Comments

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.