为什么将firebase& quot; on&"设置为"on"& quot;值& quot;当引用不存在时,不运行侦听器功能,而是“一次".& quot;值& quot;做? [英] Why does firebase "on" "value" not run the listener function when the reference doesn't exist, but "once" "value" does?

查看:31
本文介绍了为什么将firebase& quot; on&"设置为"on"& quot;值& quot;当引用不存在时,不运行侦听器功能,而是“一次".& quot;值& quot;做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我正在听一个小部件:

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!
}

如果该小部件不存在,我想知道.我希望 snapshot.val()返回 null .但是问题是该功能根本没有运行.因此,我无法对 null 进行事件检查.只是保持沉默.

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!
}

现在使用 snapshot.val() === null 运行提供给 on 的功能.这是错误还是我做错了什么?

Now the function provided to on is run with snapshot.val() === null. Is this a bug or am I doing something wrong?

推荐答案

我需要将Firebase升级到最新版本(3.0.5-> 3.2.0)

I needed to upgrade Firebase to the lastest version (3.0.5 -> 3.2.0)

npm install --save firebase

这篇关于为什么将firebase&amp; quot; on&amp;&quot;设置为"on"&amp; quot;值&amp; quot;当引用不存在时,不运行侦听器功能,而是“一次".&amp; quot;值&amp; quot;做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆