MEAN.JS stack $http.post().success() passing variables into success() anonymous function scope issue

High Owl

I'm trying to make this work:

$http.post('/route/path', {'username': $scope.threadedUsers[currentIndex].name}).
    success(function(data) {
        $scope.threadedUsers[currentIndex].ID = data._id;
        $scope.threadedUsers[currentIndex].pic = data.profile_picture[0];
    }).
    error(function(data) {
        //error stuff here
    });

$scope.threadedUsers is an array of JSON objects being dynamically populated so $scope.threadedUsers[0] = { 'ID': '', 'pic': '', 'messages': [], 'lastTimestamp': '' }

currentIndex is a local variable that refers to which index of the $scope.threadedUsers array that is currently being operated on.

The problem is that inside the success anonymous function, currentIndex is in a new scope. Now I could put currentIndex in $scope, but that seems like bad practice considering this would be the ONLY reason to do that.

Is there anyway to pass in an external value to the success callback function (for the index)? or is the only way to make currentIndex a $scope variable?

Peter Lyons

You are hitting an extremely common problem/misconception with javascript for/while loops, which are synchronous and asynchronous functions. By the time the asynchronous function, in this case the HTTP post callback executes, the synchronous loop has run to completion and the loop counter variable is already at the final ending value.

Just refactor your code into a helper method that processes a single user.

function updateUser($scope, user) {
  $http.post('/route/path', {'username': user.name}).
    success(function(data) {
      user.ID = data._id;
      user.pic = data.profile_picture[0];
  }).
  error(function(data) {
    //error stuff here
  });
}

//Here's the code you omitted but is essential to your question
var updateInScope = updateUser.bind(null, $scope);
$scope.threadedUsers.forEach(updateInScope);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AngularJS : Scope issue in $http success callback

From Dev

Passing variables from PHP to AJAX success function

From Dev

Passing variables from PHP to AJAX success function

From Dev

Success Callback not called when $http post is in method scope

From Dev

Scope not updating UI inside the success function of http.get

From Dev

angular js success response $scope variable another function

From Dev

Showing success dialog on HTTP Post

From Dev

AngularJS $http.post(...).success is not a function with file upload method

From Java

$http.get(...).success is not a function

From Dev

Passing parameter to javascript function in an Ajax success function?

From Dev

PHP scope issue in anonymous function

From Dev

HTTP Post request not executing to success or error

From Dev

Return two variables in ajax success function

From Dev

Return two variables in ajax success function

From Dev

jQuery $.post success function never fires

From Dev

jquery post success function not returning html page

From Dev

ajax success function not redirecting on success

From Dev

JQuery ajax success scope

From Dev

Why Success or Error callback function not executing properly for $http.post in angularjs

From Dev

Rickshaw JS axis render issue when called within AJAX success function

From Dev

$http success and failure comebacks, ng-repeat and accessing $scope

From Dev

$scope variable value not accessible outside of $http success callback in AngularJS

From Dev

ajaxRequest.success is not a function with smoothState.js

From Dev

Passing scope data into $http function

From Dev

jQuery Post and Redirect on Success

From Dev

Reject $http promise on success

From Dev

Angularjs - $http success vs then

From Dev

Http success not called

From Dev

Receiving 'data' of `$http.post('/someUrl', data).success` at server

Related Related

  1. 1

    AngularJS : Scope issue in $http success callback

  2. 2

    Passing variables from PHP to AJAX success function

  3. 3

    Passing variables from PHP to AJAX success function

  4. 4

    Success Callback not called when $http post is in method scope

  5. 5

    Scope not updating UI inside the success function of http.get

  6. 6

    angular js success response $scope variable another function

  7. 7

    Showing success dialog on HTTP Post

  8. 8

    AngularJS $http.post(...).success is not a function with file upload method

  9. 9

    $http.get(...).success is not a function

  10. 10

    Passing parameter to javascript function in an Ajax success function?

  11. 11

    PHP scope issue in anonymous function

  12. 12

    HTTP Post request not executing to success or error

  13. 13

    Return two variables in ajax success function

  14. 14

    Return two variables in ajax success function

  15. 15

    jQuery $.post success function never fires

  16. 16

    jquery post success function not returning html page

  17. 17

    ajax success function not redirecting on success

  18. 18

    JQuery ajax success scope

  19. 19

    Why Success or Error callback function not executing properly for $http.post in angularjs

  20. 20

    Rickshaw JS axis render issue when called within AJAX success function

  21. 21

    $http success and failure comebacks, ng-repeat and accessing $scope

  22. 22

    $scope variable value not accessible outside of $http success callback in AngularJS

  23. 23

    ajaxRequest.success is not a function with smoothState.js

  24. 24

    Passing scope data into $http function

  25. 25

    jQuery Post and Redirect on Success

  26. 26

    Reject $http promise on success

  27. 27

    Angularjs - $http success vs then

  28. 28

    Http success not called

  29. 29

    Receiving 'data' of `$http.post('/someUrl', data).success` at server

HotTag

Archive