[英]Why does firebase “on” “value” not run the listener function when the reference doesn't exist, but “once” “value” does?
Say I'm listening to a widget: 说我正在听一个小部件:
const ref = firebase.database().ref(`widgets/${widgetId}`);
ref.on('value', function(snapshot){
console.log(snapshot.val()); // <-- I never see this if ref doesn't exist!
}
If that widget doesn't exist I want to know about it. 如果该小部件不存在,我想知道它。 I'd expect snapshot.val()
to return null
. 我希望snapshot.val()
返回null
。 But the problem is the function isn't run at all. 但问题是功能根本没有运行。 So I can't event check for null
. 所以我不能检查null
事件。 It just stays silent. 它只是保持沉默。
The weird thing is, this fixes it: 奇怪的是,这解决了它:
const ref = firebase.database().ref(`widgets/${widgetId}`);
ref.once('value', function(){});
ref.on('value', function(snapshot){
console.log(snapshot.val()); // <-- Now is null!
}
Now the function provided to on
is run with snapshot.val()
=== null
. 现在提供给on
的函数与snapshot.val()
=== null
一起运行。 Is this a bug or am I doing something wrong? 这是一个错误还是我做错了什么?
我需要将Firebase升级到最新版本(3.0.5 - > 3.2.0)
npm install --save firebase
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.