2017-06-01 288 views
0

我查詢FB在我下面的函數:火力地堡ORDERBY查詢返回null

getLimit: function(artistId, limit) { 
    console.log(FBURL + 
         'songs?orderBy=\"artist_timestamp\"&startAt=\"' + 
         artistId + 
         '_\"&endAt=\"' + 
         artistId + 
         '_9999\"&limitToLast=' + 
         limit + 
         ''); 

    ref= new Firebase(FBURL + 
         'songs?orderBy=\"artist_timestamp\"&startAt=\"' + 
         artistId + 
         '_\"&endAt=\"' + 
         artistId + 
         '_9999\"&limitToLast=' + 
         limit + 
         ''); 

    console.log(ref); 

     ref.on('value', (snapshot) => { 
     console.log("hello"); 
     var val = snapshot.val(); 
     console.log(val); 
    }); 
    results = $firebaseArray(ref); 
     results.$loaded().then(function() { 
    console.log("loaded record:", results.$id); 

    // To iterate the key/value pairs of the object, use angular.forEach() 
    angular.forEach(results, function(value, key) { 
     console.log(key, value); 
    }); 
}); 
    return results; 
    } 

當我看到我對第一console.log我試圖通過複製和粘貼重現null導致終端呼叫什麼網址在逐字參數new Firebase()成捲曲要求如下:

curl "the-exact-url-except-add-dot-json" 

這是我所傳遞給new Firebase()的基本格式:

https://my-proj.firebaseio.com/songs?orderBy="artist_timestamp"&startAt="foo_"&endAt="foo_9999"&limitToLast=7 

所以當我蜷縮

curl 'https://my-proj.firebaseio.com/songs.json?orderBy="artist_timestamp"&startAt="foo_"&endAt="foo_9999"&limitToLast=7' 

它的工作原理,但是當我把前者URL的快照我在on('value')回調

得到null和我得到的結果,我希望這是一個對象song鍵和對象。

然而,當我得到的ref以及轉換爲firebaseArray並調用回調$loaded後兩個快照我得到在這兩種情況下null。我不確定發生了什麼事情會使結果出現在終端中,但不在代碼中。

回答

1

您無法將包含查詢參數的網址傳遞到Firebase構造函數中。相反,您可以通過調用位置引用上的相應方法來添加它們。例如。

ref = new Firebase(FBURL + 'songs'); 
var query = ref.orderByChild("artist_timestamp") 
       .startAt(artistId) 
       .endAt(artistId+'_9999') 
       .limitToLast(limit); 

我也高度建議您遷移到「新」火力地堡SDK,這是記錄在這裏:https://firebase.google.com/docs/database/web/start。使用更新的SDK(您使用的SDK至少一年)將確保您找到更多有用的文檔,並且有更多的開發人員可以提供幫助。