javascript - Firebase 连接状态监听器在 javascript 中返回 false

标签 javascript firebase

每次我运行此脚本时,它第一次显示“未连接”,然后显示“已连接”。为什么总是先触发“未连接”? 有一些主题与此问题相同 link ,但没有答案。

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

最佳答案

这是 Firebase 的一个正常的、有目的的功能,可以避免任何竞争条件。

引用文档: https://firebase.google.com/docs/database/web/offline-capabilities

Note that your app should queue the disconnect operations before a user is marked online, to avoid any race conditions in the event that the client's network connection is lost before both commands can be sent to the server.

关于javascript - Firebase 连接状态监听器在 javascript 中返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46294783/

相关文章:

firebase - Flutter处理调试控制台错误 'Method not found'

json - 在 Swift Firebase 中滚动时分页

javascript - VIdeo不会以证明内容为中心

javascript - 操作对象数组时出现 TS2345 错误

asp.net - 这段代码可以工作吗? Javascript/asp.net(代码不起作用)

ios - 问题 找不到自动链接框架 'FirebaseMessaging'

javascript - 在这种情况下,为什么我必须单击两次才能加载图像?

javascript - 有人能解释一下这个 JavaScript 之谜吗?

ios - 应用程序激活时快速 View 重新加载

javascript - 迭代 Firebase ref 以加载其子项