我在ionic2上使用Firebase,如下所示:
firebase.database()
.ref("my_ref")
.orderByChild("my_field")
.equalTo(variable_field)
.on("value", (snapshot) => {
alert('Twice?');
});
为什么我的alert('Twice?')
显示两次?如何避免这些多次调用?
它
被调用两次,因为on
使用所请求数据的当前值调用回调,并在数据更改时再次调用它。
如果只想调用一次回调,请改用 once
方法:
firebase.database()
.ref("my_ref")
.orderByChild("my_field")
.equalTo(variable_field)
.once("value", (snapshot) => {
alert('Once!');
});