为什么Firebase“打开” "值QUOT;当引用不存在时不运行侦听器函数,而是“一次” "值QUOT;呢? [英] Why does firebase "on" "value" not run the listener function when the reference doesn't exist, but "once" "value" does?
本文介绍了为什么Firebase“打开” "值QUOT;当引用不存在时不运行侦听器函数,而是“一次” "值QUOT;呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ $ p $
const ref = firebase.database()。ref(`widgets / $ {}为widgetid`);
ref.on('value',function(snapshot){
console.log(snapshot.val()); //< - 如果ref不存在,我永远不会看到!$
如果这个小部件不存在,我想知道它。期望 snapshot.val()
来返回 null
但问题是函数根本不运行。我不能检查 null
。它只是保持沉默。
奇怪的是,这个修复了它:
const ref = firebase.database()。ref(`widgets / $ {widgetId}`);
ref.once('value',function(){});
ref.on('value',function(snapshot){
console.log(snapshot.val()); //< ; - 现在是空的!
}
现在提供给<$ c $使用 snapshot.val()来运行c> on
。
=== null
。这个bug或者我做错了什么? sion(3.0.5 - > 3.2.0) npm install --save firebase
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
. But the problem is the function isn't run at all. So I can't event check for 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
. Is this a bug or am I doing something wrong?
解决方案
I needed to upgrade Firebase to the lastest version (3.0.5 -> 3.2.0)
npm install --save firebase
这篇关于为什么Firebase“打开” "值QUOT;当引用不存在时不运行侦听器函数,而是“一次” "值QUOT;呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文