2016-08-25 3 views
0

Firebase에서 두 가지 별도의 쿼리를 수행하려고합니다. 첫 번째는 저장된 총 항목 수를 가져 오는 일반적인 쿼리입니다. 그건 제대로 작동합니다.Firebase 날짜 범위 쿼리가 작동하지 않습니다.

두 번째 것은 올해 기준으로 저장된 모든 항목을 결정하는 시간 기반 범위 쿼리입니다.

첫 번째 쿼리가 작동합니다. 두 번째는 콘솔에 "null"을 표시합니다. 모든 쿼리가 여기에 첫 번째 항목과 마지막 항목에서 타임 스탬프를 살펴 인 2016 년

var ref = new Firebase('https://xxxxxxxx.firebaseio.com/entries'); 
    ref.once('value', function(snapshot) { 
    $scope.allEntries = snapshot.numChildren(); 
     console.log('Total entries saved to date: ' + $scope.allEntries); 
     // shows 10 entries, which is correct 
    }); 

var ref2 = new Firebase('https://xxxxxx.firebaseio.com/'); 
ref2.child('entries') 
    .startAt(14516244) // January 1st, 2016 
    .endAt(1472131318) // current date (aug, 25th 2016) 
    .once('value', function(snapshot) { 
      console.log(snapshot.numChildren()); 
      // shows "null" 
}); 

에서 만든 그것은 또한 "10"를 표시해야합니다.

enter image description here

답변

5

당신은 당신의 두 번째 쿼리에 orderByChild()가 누락되었습니다.

하면 쿼리의 시작 부분에 추가 :

ref2.child('entries') 
    .orderByChild('createdAt') 
    .startAt(14516244) // January 1st, 2016 
    .endAt(1472131318) // current date (aug, 25th 2016) 
    .once('value', function(snapshot) { 
      console.log(snapshot.numChildren()); 
      // shows "null" 
}); 

편집 :

두 번째 문제는 그들이 초에있는 당신의 createdAt가에 있기 때문에 쿼리에 날짜가 너무 작은 것입니다 밀리 초. 그것을 변환하십시오.

ref2.child('entries') 
    .orderByChild('createdAt') 
    .startAt(14516244000) // January 1st, 2016 
    .endAt(1472131318000) // current date (aug, 25th 2016) 
    .once('value', function(snapshot) { 
      console.log(snapshot.numChildren()); 
      // shows "null" 
}); 
+0

이제 "null"대신 "0"이 표시됩니다. "10"이어야합니다. – Livi17

+0

문제가있는 곳을 안다. 나는 잠시 후에 대답을 편집 할 것이다. – pr0gramist

관련 문제