2017-10-20 11 views
0

를 사용하여 특정의 아이의 모든 아이를 얻을 : 내가 임의의 문을 선택해야번 다음과 같이 나는 중포 기지 데이터베이스가 중포 기지 웹

enter image description here

. firebase 'push'키 (다른 SO 응답 기반)를 기반으로 임의의 레코드를 선택하는 데 어려움으로 인해 숫자 키 "n : 1"등을 만들어서 임의의 숫자를 기반으로 호출 할 수 있습니다. 다음은 임의의 문을 검색하는 코드입니다.

 function getStatement() { 
     var ref= firebase.database().ref('/totNumState/'); 
     ref.once('value').then(function(snapshot) { 
      var max = (snapshot.val()); 
      var randomIndex = Math.floor(Math.random() * max) + 1; 
      var rootRef = firebase.database().ref(); 
      rootRef.orderByChild('data').equalTo('n:'+randomIndex).once("value") 

       .then(function(snapshot) { 
       console.log(snapshot.val()); 
       }); 
     }, function(error){console.error(error)}); 
    } 

문제 : 검색 결과에 'null'응답이 표시됩니다. MAIN QUESTION : firebase web에서 특정 어린이의 모든 자녀를 어떻게 얻을 수 있습니까? SECONDARY QUESTION (관련) : 내가 검색하고자하는 레코드를 정확히 알고있을 때 firebase이 (자원이 부족한) 'orderByChild'를 사용하게 만드는 이유는 무엇입니까?

덕분에 그 조언에 따라 답을 찾고 다른 사람, 작업 솔루션을

+0

'데이터'라는 이름의 자식 노드가 여러 개 있습니까? 그것이 바로 지금 코드가 찾고있는 것이기 때문입니다. 원하는 특정 노드를 알고 있다면'orderByChild()'를 사용할 필요가 없습니다.'firebase.database.ref ('data/n :'+ randomIndex) '와 같이 직접 참조하십시오. –

+0

Jen. 게다가'orderByChild()'를 사용할 필요가 없다는 것을 아는 데 유용합니다. – user2206361

답변

0

은 다음과 같습니다 위의 젠에

function getStatement() { 
     var ref= firebase.database().ref('/totNumState/'); 
     ref.once('value').then(function(snapshot) { 
      var max = (snapshot.val()); 
      var randomIndex = Math.floor(Math.random() * max) + 1; 

      var st = firebase.database().ref('data/n:'+randomIndex) 
      st.once("value") 
       .then(function(snapshot) { 
       console.log(snapshot.val().statement); 
       }); 
     }, function(error){console.error(error)}); 
    } 

감사합니다.