Firebase ref.on("value"即使没有值更改也被调用-node.js [英] Firebase ref.on("value" is getting called even without a change in value - node.js

查看:38
本文介绍了Firebase ref.on("value"即使没有值更改也被调用-node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试观察我的Firebase数据库中的更改.

I am trying to observe a change in my firebase DB.

这是我的结构

数据库-身份验证:正确

DB - Authenticate : true

我正在我的node.js应用程序中收听身份验证"中的更改

I am listening to changes in Authenticate like this in my node.js app

var authRef = firebase.database().ref('Authentication');
      authRef.on('value', function(snapshot) {
              console.log(snapshot.val());
              res.send(snapshot.val());
      });

但是,一旦我调用 authRef.on ,它就会获取先前的值,而不是等待更改.

But as soon as I call authRef.on , it is fetching the previous value instead of waiting for a change.

请咨询.

推荐答案

这是Firebase侦听器的本质.
创建值侦听器时,它将至少获取一次数据.
为了防止这种情况,我将执行以下操作:

It is the nature of firebase listeners.
When you create a value listener it will fetch the data at least once.
I would do the following to prevent this:

var i = 0
var authRef = firebase.database().ref('Authentication');
  authRef.on('value', function(snapshot) {

          if( i != 0){
            console.log(snapshot.val());
            res.send(snapshot.val());
          }
          i++
  });

如果您这样做,它将不会考虑第一个电话

It will not consider the first call if you do so

这篇关于Firebase ref.on("value"即使没有值更改也被调用-node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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