2016-08-25 31 views
0

Firebaseから2つの別のクエリを実行しようとしています。最初は、保存されたエントリの総数を取得する一般的なクエリです。それは正しく動作します。Firebaseの日付範囲クエリが機能しない

今年だけのために保存されたすべてのエントリを決定する時間ベースの範囲クエリであると考えられます。

最初のクエリが機能しています。 2番目はコンソールに「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

あなたは2番目のクエリで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

私はどこに問題があるかを知っています、私は2番目に答えを編集します。 – pr0gramist

関連する問題