0

I have an array with object in $scope in angularjs. I successfully push the object into array. It shows array is form in console but I can't retrieve the last object of array like normal array in javascript. The following is my code.

$scope.messages = [];

$http.get("http://localhost/php/retrieve_message.php").success(function (data)
{
    for (i = 0; i < data.length; i++)
    {
        $scope.messages.push(data[i]);
        console.log("success");
    }
}).error(function ()
{
    console.log("error");
});
console.log([$scope.messages); console.log($scope.messages[$scope.messages.length - 1]);
5
  • 2
    $http is an async method. If you console.log out of them, it's return empty or undefined. Commented Sep 28, 2016 at 7:28
  • but I console with console.log($scope.messages); it display a list of object. The problem now is I want to get last object in $scope.messages array Commented Sep 28, 2016 at 7:30
  • It's is like @Davide said - the method is async, which means you cannot console.log it outside the get method and suspect that it has already finished pulling data from the server at that point. Commented Sep 28, 2016 at 7:32
  • Yeah as David said $http is async which means it will fire the http request and go ahead with executing the rest of the code. Once the response is received execution will come to the success/failure function. Commented Sep 28, 2016 at 7:35
  • @ŁukaszTrzewik thanks. Learn new thing today Commented Sep 28, 2016 at 7:46

4 Answers 4

2

This is correct.

$scope.messages = [];

$http.get("http://localhost/php/retrieve_message.php").success(function(data) {
    for (i = 0; i < data.length; i++) {
        $scope.messages.push(data[i]);
        console.log("success");
    }
    console.log($scope.messages);
    console.log($scope.messages[$scope.messages.length - 1]);
}).error(function() {
    console.log("error");

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

4 Comments

Thanks, Problem Solved
@chanyoonghon Accept my answer so.
But if I want to use the array in other place after $http. Can I do that?
@chanyoonghon you can use your array into success or you can think about github.com/caolan/async
0

You are making an Asynchronous request.

In this case, you receive a callback when the data has been received, which lets the browser continue to work as normal while your request is being handled.

 $scope.messages = [];

  $http.get("http://localhost/php/retrieve_message.php").success(function(data){
       for(i=0; i<data.length; i++){
            $scope.messages.push(data[i]);
             console.log("success");
       }
    // put your code in success method
    console.log($scope.messages);
    console.log($scope.messages[$scope.messages.length - 1]);

    }).error(function() {
        console.log("error");
    });

In your case you are accessing the list before the data has been received. So the length of $scope.messages is 0 and you are trying to get $scope.messages[-1]

You can learn more about Synchronous and asynchronous requests here

Comments

0
$scope.messages = [];

  $http.get("http://localhost/php/retrieve_message.php").success(function(data){
       // this callback function gets executed only after server returns a successful response. So, here you have access to data you have got from the server.
       for(i=0; i<data.length; i++){
            $scope.messages.push(data[i]);
             console.log("success");
       }
       console.log([$scope.messages);
       console.log($scope.messages[$scope.messages.length - 1]);
    }).error(function() {
        console.log("error");
    });
    // Following code executes before even server returns data.
    //console.log([$scope.messages);
    //console.log($scope.messages[$scope.messages.length - 1]);

Comments

-1

Try by updating your for loop as :

for(var x in data){
    $scope.messages.push(x);
    console.log("success");
}

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.