2016-07-31 69 views
3

只是一個小問題,刪除值:
現在我有這個結構
如何火力

images 
---- uniqueId 
-------- id_logement : 1747657 
-------- image : dataimage 
---- uniqueId 
-------- id_logement : 1747657 
-------- image : dataimage 
---- uniqueId 
-------- id_logement : 985445234 
-------- image : dataimage 

而且它的更好!謝謝 !!但:
如何刪除id_logement = 1747657的所有圖像?
我試過

 firebase.database().ref('logements/'+key).remove(); 
     firebase.database().ref('geofire/'+key).remove(); 
     firebase.database().ref('images').child('id_logement').equalTo(key).remove(); 

與關鍵= 1747657但沒有成功的圖片!這UniqueId讓我緊張!請你能給我多些建議嗎?非常感謝你

+0

嘿巴勃羅,只是增加了一個答案。對延遲抱歉。問候。 :) – adolfosrs

回答

6

,你需要首先檢索並刪除它設置其值爲null並承諾更改爲update

let ref = firebase.database().ref('images'); 
ref.orderByChild('id_logement').equalTo(key).once('value', snapshot => { 
    let updates = {}; 
    snapshot.forEach(child => updates[child.key] = null); 
    ref.update(updates); 
}); 

工作jsFiddle

+0

這就是我一直在尋找的!現在我可以在一次中刪除(設置爲空)!非常感謝你 ! –

+0

如果我們想要刪除特定的數據節點,該怎麼辦? – BhargavSushant

+0

@BhargavSushant ref('node')。remove()? – adolfosrs

1

試試這個代碼: - 既然你要基於查詢到批量刪除數據

rootRef.child("images").addListenerForSingleValueEvent(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot snapshot) { 
      for(DataSnapshot dataSnapshot : snapshot.getChildren()) 
      { 
       if(dataSnapshot.child("id_logement").getValue().toString().equals("1747657")) 
       { 
        dataSnapshot.getRef().setValue(null); 
       } 
      }   
     } 
     @Override 
     public void onCancelled(FirebaseError firebaseError) { 
     } 
    }); 
+1

很好的答案。但是OP正在尋找一種JavaScript解決方案,由於缺乏平臺標籤,所以很容易忽略。 –

3

實際上有一個更簡單的方法。

只要打電話給ref財產快照,並使用.on('child_added',...)

var ref = firebase.database().ref('images'); 
ref.orderByChild('id_logement').equalTo(key).on('child_added', (snapshot) => { 
    snapshot.ref.remove() 
}); 
+0

好的。儘管這使得代碼看起來很小,但我認爲這不是性能方面的最佳選擇,並且可能導致不期望的行爲。這個代碼將爲每個被移除的子節點調用'remove',而我提供的解決方案只調用一次'update'。另外,由於這是一個觀察者,您將會監聽任何其他添加的孩子,並將刪除剛剛添加的節點。 – adolfosrs