2017-07-11 37 views
0

我有一個數據結構,像這樣:火力地堡監聽器,爲特定子值,對兒童尚未創建

friend_requests_sent: 

    userId1: 
     sub_userId1: 
      status: "request_sent" 
     sub_userId2: 
      status: "request_sent" 
     sub_userId3: 
      status: "request_failed" 

    userId2: 
     sub_userId1: 
      status: "request_sent" 
     sub_userId2: 
      status: "request_failed" 
     sub_userId3: 
      status: "request_sent" 

每個sub_userId在某個時間點被創建,但我想建立一個數據庫監聽器只有當status的值設置爲request_sent時,纔會觸發sub_userId

即,我試圖創建不存在的(但)路徑/friend_requests_sent/userId1/sub_userId4/status觀察員將檢查它是否確實request_sent都被創建,並在它時,status值的變化之後。

我該如何找回前者的快照?感覺就像我迷失在火力點查詢系統中一樣。

在此先感謝!

+0

這聽起來像是一個firebase雲功能的工作。你想要做什麼時,該路徑改變...你想更新數據庫的某些部分... –

+0

不,我想更新用戶界面 – Herakleis

回答

0
friend_requests_sent: 
    userId1: 
     requests: 
      sub_userId1: "insertstatushere" 
      sub_userId2: "insertstatushere" 
      //continue 

我覺得這會好很多。只添加,刪除並更改該路徑的子項(userIDHere/requests /)。這樣你可以檢查和處理一切。

+0

謝謝,但我有其他東西在那裏比「狀態」字段(timestamp),所以我猜想不能這樣做:s – Herakleis

+0

該評論實際上是您的問題的關鍵:您似乎想要基於條目的狀態顯示列表。在這種情況下,您應該精確地將其保存在數據庫中:包含條目狀態的列表。即使您還將狀態數據保存在其他地方,要將其顯示在UI中,從數據庫到UI的最直接映射可能是最容易維護的。 –

0

這裏是代碼,我建議:

firebase.database().ref("friend_requests_sent").once('value', function(snap){ 

    snap.forEach(function(child){ 
     //For each userID below query looks for subusers whose status are 'request_sent' 

     child.ref.orderByChild('status').equalTo('request_sent').on('child_added', function(grandChild){ 
      //grandChild.val() is the sub-user of child.val() 
      //grandChild.val().status is equal to 'request_sent' 
      alert(grandChild.val().status); 
     }); 
    }); 

)}; 

希望的作品!