【问题标题】:Can not access firebase Realtime database in my web app无法在我的网络应用程序中访问 firebase 实时数据库
【发布时间】:2018-10-01 06:03:15
【问题描述】:

我正在开发 firebase webapp,但遇到了一个问题。 我无法访问(读/写)firebase 实时数据库。

 <script src="https://www.gstatic.com/firebasejs/4.12.0/firebase.js"></script>
<script>
var config = {
    apiKey: "AIzaSyCPYu_iqHjNoXjMfdMVLoYs5Graf2gdz7I",
    authDomain: "sharing-is-caring-967c8.firebaseapp.com",
    databaseURL: "https://sharing-is-caring-967c8.firebaseio.com",
    projectId: "sharing-is-caring-967c8",
    storageBucket: "sharing-is-caring-967c8.appspot.com",
    messagingSenderId: "884406512169"
  };
firebase.initializeApp(config);

    firebase.auth().signInWithEmailAndPassword("myemail@gmail.com", "Qqqq123").then(function(info) {
        var uid = info.uid;
        firebase.database().ref('users/' + uid + '/userDetails').once('value').then(function(data) {
            alert(data)
        }).catch(function(error) {
            alert(error.message);
        });
    }).catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        alert(errorMessage);
        // ...
    });
</script>

SignInWithEmailAndPassword 工作正常。 在数据库部分, once('value') 函数不再响应。 没有成功,也没有失败。 没有任何错误消息并且始终保持沉默

实时数据库规则

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null",
      "threads": {
        "$tid": {
          "messages": {
            ".indexOn": ["read", "date"]
          }
        }
      }
  }
}

Fire base Web Setup Guide Screenshot of my Firebase

数据库结构

{
  "users" : {
    "BzucCBivhiRUogK5GeNrMV6k6M83" : {
      "userDetails" : {
        "displayName" : "Stanislav Sergeev",
        "email" : "stanislav.atos@yahoo.com",
        "notifications" : true,
        "phoneNumber" : "",
        "profile_image" : "",
        "pushToken" : "ec7iJ7Mt7Lg:APA91bFb8NK2XeaLVUgr-DEPRFuLMDCk9_tIr_eHBjmuZPnHDUvFuCsj3kez4F5-Y9CjVXOY6VlsFi5nlazSZys4KBjwCKcpINbV5S2ZEaH09aWZ6sLga8dOja-UIp3Iz3DSOAb4bcH4"
      }
    }
  }
}

【问题讨论】:

  • 您的索引规则中似乎有一个错字:$tid 而不是 $uid
  • 等一下,我会附上。你以前有没有遇到过这个问题?
  • @Stanislas Sergeev 请注意,您可以通过单击 Firebase 数据库控制台中的“导出”按钮轻松获取数据库的文本导出 (JSON)
  • 但是数据太多
  • @Stanislas Sergeev 您可以删除大部分数据并保留一组显示结构的“有意义”节点

标签: javascript firebase firebase-realtime-database


【解决方案1】:

我之前也遇到过同样的问题,这与您的位置有关。 请尝试使用VPN。

【讨论】:

  • 它与firebase会员资格有关。免费版本有时在某些地方不起作用。我不知道明确的位置,但就是这样。
【解决方案2】:

所以,根据 cmets 和您的数据库结构:

首先,我不明白以下规则的用途是什么?

 "threads": {
    "$tid": {
      "messages": {
        ".indexOn": ["read", "date"]
      }
    }
  }

其次,您可以尝试使用基本规则吗:

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

第三,你确定你被监视了吗?如果你这样做,你会得到什么

var uid = info.uid;
console.log(uid);

PS:请注意,您使用的方法显然将被弃用,请参阅 https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInWithEmailAndPassword


编辑:这是有效的,我已经复制了你的案例。注意returndata.val()

firebase.auth().signInWithEmailAndPassword("azerty@nmw.com", "****")
    .then(function (info) {
        var uid = info.uid;
        console.log(uid);
        return firebase.database().ref('users/' + uid + '/userDetails').once('value');
    })
    .then(function (data) {
        console.log(data.val());
    })
    .catch(function (error) {
        alert(error.message);
    });

【讨论】:

  • @StanislavSergeev 如果您使用简单的规则集进行查询会发生什么?
  • 没有发生。我改为简单的规则,但它不起作用重要的是我也有 iOS 应用程序,即使网络应用程序不起作用它也能工作
  • @StanislavSergeev 请再试一次。它对我来说非常有效,具有与您相同的数据结构和简单的规则(即“.read”:“auth!= null”,“.write”:“auth!= null”)。请注意,在我的代码中,输出是在浏览器控制台中完成的,而不是通过警报。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-20
  • 1970-01-01
  • 2017-09-24
  • 2021-11-06
  • 2021-09-14
相关资源
最近更新 更多