Firebase的实时数据库的`child_removed`事件未触发

时间:2020-10-14 13:22:30

标签: javascript firebase firebase-realtime-database

let database = firebase.database(); //the database is empty.

database.ref('user').set({
    color: "red",
    height: 180,
});

database.ref('user/color').remove();

database.ref('user').on('child_added', e => {
    console.log(e.val());
});

database.ref('user').on('child_removed', e => {
    console.log(e.val());
});

我希望'child_removed'会因为以下原因而被触发:database.ref('user/color').remove();,并且我希望它注销'red'

'child_added'的工作方式似乎是这样的:它会为每个最终产生的值user触发。因此,唯一记录到控制台的是180

1 个答案:

答案 0 :(得分:1)

问题是您在设置侦听器之前先致电.remove()。将您的听众之前呼叫.remove()

let database = firebase.database(); //the database is empty.

database.ref('user').set({
    color: "red",
    height: 180,
});

// listener first
database.ref('user').on('child_removed', e => {
    console.log(e.val());
});

// then calls
database.ref('user/color').remove();