2016-09-22 21 views
0

我有一些按鍵的數組:
火力地堡簡單的查詢日期範圍...不起作用

['-KSH1rJ8BgbOWKdwdn0J', '-KSH0B8MfPCB2_zI8H3Q', ...]; 

併爲每個關鍵,我想知道,如果「ts_exp」,這是過期的時間戳在昨天和明天之間都是時間戳。

var yesterday = moment().subtract(1, 'day').format("x"); 
var tomorrow = moment().add(1, 'day').format("x"); 

foreach(... as key){ 
     var rootRef = firebase.database().ref('demandes').child(key) 
     .orderByChild('ts_exp') 
     .startAt(yesterday) 
     .endAt(tomorrow); 

     rootRef.once("value", function(messageSnapshot) { 
      console.log(messageSnapshot.val()); // always return null 
     }); 
    } 

這有什麼錯此查詢?這是始終爲空(但我有一個「demade」,這兩個值之間有一個「ts_exp」)。我也在我的firebase規則中做了.indexOn「ts_exp」。
謝謝
這是數據結構:

-demandes 
    ----- -KSH1rJ8BgbOWKdwdn0J 
    ---------- ts_exp : 14123123123, 
    ---------- title : "pipo1", 
    ---------- desc : "desc" 
    ----- -KSH0B8MfPCB2_zI8H3Q 
    ---------- ts_exp : 14123176576, 
    ---------- title : "pipo2", 
    ---------- desc : "desc2" 

回答

1

看起來你ts_exp值是數字(14123123123),但你的startAt/ENDAT是字符串,火力地堡數據庫中的所有字符串之前排序的所有號碼,所以你的查詢正在搜索一個沒有數據的範圍。

未經檢驗的,但嘗試:

var yesterday = moment().subtract(1, 'day').valueOf(); 
var tomorrow = moment().add(1, 'day').valueOf(); 
+0

謝謝你,但是,我刪除'.child(密鑰)',並與所有的'demandes'路徑我昨天和明天做值查詢,數據是正確的顯示並過濾。所有我想知道的(不加載所有'demandes'數據)是如果一個具有特定鍵的需求在這個範圍之間。 –

+1

你不能明智地比較字符串和數字。將輸出從moment.js轉換爲數字,例如用'.startAt(parseInt(昨天))'。請參閱http://jsbin.com/noziri/edit?js,console –

+0

哇,感謝你M.普菲倫。我試過'firebase.database().ref('demandes')。child(key).orderByChild('ts_exp') .startAt(parseInt(yesterday)) .endAt(parseInt(tomorrow));'but still the相同的結果... –