2014-10-21 3 views
0

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"); 
    }); 

하지만 배열의 정보를 얻을 수있는 문제가 있습니다 응답에서

TypeError: Cannot read property 'all' of undefined 

내가 가진 : 당신의 도움에 대한

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

감사합니다! 당신은 인덱스 누락

+0

응답은 객체의 배열입니다. JSON 결과 개체에 따르면 첫 번째 배열 안에 있습니다. 인덱스 응답 [0] .result.all –

답변

1

귀하의 response 2 객체의 배열로 나타납니다.

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

으로 :

는 교체

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

이 응답에 두 개체를 반복해야하고, 관련 단어를 기록합니다.

+0

감사하지만 "words : undefined"오류가 있습니다. "단어"로 반복해야합니까? – Carlos

+0

즉'response [i] .result.all'에는'Word'라는 속성이 없습니다. 'console.log ("words :", response [i] .result.all);'시도해보십시오. – Cerbrus

+0

@Carlos 제 생각에 응답 [i] .result.all [0]' – iJade

0

은, 아래의 코드를 시도 :

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

을 추가하지 마십시오. 그렇게 쓰지 마세요. – Cerbrus

+0

@Cerbrus가 업데이트되었습니다. – iJade

관련 문제