Firebase连接状态侦听器在javascript中返回false [英] Firebase connection state listener returns false in javascript

查看:43
本文介绍了Firebase连接状态侦听器在javascript中返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我运行此脚本时,它都会在第一次连接"之后显示未连接".为什么总是总是先触发未连接"? 有些主题具有相同的问题,例如这样的链接,但没有答案.

Everytime I run this script, it shows "not connected" first time and after that "connected". Why does it always trigger "not connected" first? There are some topics with same issue like this one link, but no answers.

var connectedRef = firebase.database().ref(".info/connected");
connectedRef.on("value", function(snap) {
  if (snap.val() === true) {
    alert("connected");
  } else {
    alert("not connected");
  }
});

推荐答案

页面加载时,Firebase客户端最初未连接到其后端服务器.因此它将.info/connected标记为false.

When your page load, the Firebase client is initially not connected to its backend servers. So it marks .info/connected as false.

然后开始建立连接,这可能需要一些时间.如果/当建立连接时,它将.info/connected标记为true.

It then starts building the connection, which may take some time. If/when it established the connection it marks .info/connected as true.

这是预期的行为.如果它给您的应用造成了问题,则可能是您试图将指示器用于可能不适合的应用.如果是这样,如果您共享用例,可能会更容易获得帮助.

This is the expected behavior. If it is causing problems for your app, it's likely that you're trying to use the indicator for something it may not be directly suitable for. If that is the case, it's probably easier to help if you share your use-case.

这篇关于Firebase连接状态侦听器在javascript中返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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