Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share
    
response is an array of objects. According to the JSON result object is inside the first array. Try adding an index response[0].result.all –  Abhishek Prakash 20 mins ago

2 Answers 2

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.

share|improve this answer
    
Thanks but still have an error "words: undefined" . I have also to iterate with "Words"? –  Carlos 13 mins ago
    
That means that response[i].result.all does not have a property called Word. Try console.log("words: ", response[i].result.all); to see what it does contain. –  Cerbrus 11 mins ago
    
@Carlos i think it's response[i].result.all[0] –  iJay 11 mins ago
    
@Cerbrus OOoops you are right. Many thanks, it works ! –  Carlos 10 mins ago
    
@Carlos was dat for me –  iJay 8 mins ago

You are missing index, try the below code:

response[i].result.all[j]   where j=0....n
share|improve this answer
    
U r not txtng. Please don't write like that. –  Cerbrus 21 mins ago
    
@Cerbrus updated –  iJay 20 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.