0
我試圖從不斷更新的孩子的Firebase數據中檢索JSON數據。快照鍵在每次提交新數據時都會更改。 (使用sparkpost將數據發送到firebase)。我正試圖找出一種方法來構建我的檢索,以便從最近的鍵中獲取。從更改快照鍵中檢索特定數據
var ref = new Firebase("https://watspark.firebaseio.com/raw-events/");
var _url = ('https://watspark.firebaseio.com/raw-events/');
// Retrieve new posts as they are added to our database
ref.on("child_added", function(snapshot, prevChildKey) {
var newPost = snapshot.val();
console.log(snapshot.val());
// console.log("text: " + newPost.text);
// console.log("Title: " + newPost.title);
// console.log("Previous Post ID: " + prevChildKey);
// var fireUrl = ("https://watspark.firebaseio.com/raw-events/" + snapshot.val() + "/0/msys/relay_message/content");
// var bigBoy = new Firebase(fireUrl);
// console.log("text: " + bigBoy.text);
var param = snapshot.key();
console.log(param);
});
ref.orderByChild("0/mysys/relay_message/content/text").on("child_added", function(snapshot) {
console.log(snapshot.key() + "was" + snapshot.val().text);
});
我的JSON數據的結構是這樣的:
snapshot.val();
{
0 {
mysys{
relay_message{
content
我怎麼會去這樣做呢? 感謝
這取決於你的「最近」的定義。當您查詢使用推式ID填充的列表時,您可以使用'ref.orderByKey()。limitToLast(1)'獲得最新的。但請注意,這對於短列表來說可以很好地工作,但隨着列表的增長,它會變得越來越慢(請參閱[this](http://stackoverflow.com/questions/34482615/database-io-capacity-in-firebase-usage -metrics-analytics-tab/34482768#34482768)和[this](http://stackoverflow.com/q/35165815/))。如果你只關心最新的項目,最好保留'latestItemKey:「-Khasdgsagd」並更新你添加的每個新項目。 –
的權利,但我仍然很難通過最新的項目關鍵字進入firebase的URL,以便我可以檢索我的文本字段3個級別到我的json數據 – bretth18