以下代碼會產生無限循環的重複的child_added,儘管我從不添加孩子。節點被刪除。我只是永遠循環。我怎樣才能解決這個問題?爲什麼我要刪除child_added事件?
//notifications branch is pre-populated when I get here:
notifications.startAt(null).on('child_added', processNotification, logError);
function processNotification(notification)
{
removeNotification(notification);
}
function removeNotification(notification)
{
var child = notifications.child(notification.name());
console.log("REMOVING", child.toString());
child.remove();
}
我記錄是相同的四個節點(A,B,C,d)被移除:
REMOVING https://my.firebaseio.com/experimental/-JVMi0_4poXfOWUt5g49 - A
REMOVING https://my.firebaseio.com/experimental/-JVMi1Y_bFwZAkffRel4 - B
REMOVING https://my.firebaseio.com/experimental/-JVMi2lyhKj8z27ik71x - C
REMOVING https://my.firebaseio.com/experimental/-JVMhzazdgHYAstqxu8L - D
REMOVING https://my.firebaseio.com/experimental/-JVMi0_4poXfOWUt5g49 - A
REMOVING https://my.firebaseio.com/experimental/-JVMi1Y_bFwZAkffRel4 - B
REMOVING https://my.firebaseio.com/experimental/-JVMi2lyhKj8z27ik71x - C
REMOVING https://mu.firebaseio.com/experimental/-JVMhzazdgHYAstqxu8L - D
...
當你用var child = notification.ref()替換'var child = notifications.child(notification.name());'' – 2014-08-27 17:34:31
呃,這個問題是回調的不是引用。刪除發生。如果它有效,我不會看到這怎麼重要。你的想法是什麼? – user9170 2014-08-27 17:43:40
我的推理是它看起來很複雜,我沒有時間重現這個問題。或者,您也可以查看https://github.com/firebase/firebase-work-queue,它可以做類似於您嘗試的操作。他們使用返回null的'transaction'處理程序來移除那裏的處理項目。 – 2014-08-27 18:29:46