Carlos
Carlos

Reputation: 79

AngularJS - Controller JavaScript $http.get

I have the following code to get info from a JSON.

$http.get('http://localhost:3000/folder/'Id)
     .success(function (response) {
           console.log("response ", response);
           console.log("words: ", response.result.all.Word);
     })
     .error(function (response) {
           console.log("error");
     });

But I have a problem to get info in the array:

TypeError: Cannot read property 'all' of undefined

In response I have:

response  [Object, Object]
  0: Object
    _id: "543e95d78drjfn38ed53ec"
     result: Object
       all: ObjectWord: Array[17]
        0: "word1"
        1: "word2"
        2: "word3"
         ...

Thanks for your help!

Upvotes: 0

Views: 48

Answers (2)

iJade
iJade

Reputation: 23791

You are missing index, try the below code:

response[i].result.all[j]   where j=0....n

Upvotes: 0

Cerbrus
Cerbrus

Reputation: 72857

Your response appears to be an array of 2 objects.

Replace:

console.log("words: ", response.result.all.Word);

With:

for(var i = 0; i < response.length; i++){
    console.log("words: ", response[i].result.all.Word);
}

This should iterate over both objects in the response, and log the related word.

Upvotes: 1

Related Questions