【问题标题】:Cannot read all data from firebase无法从 firebase 读取所有数据
【发布时间】:2017-06-21 01:47:19
【问题描述】:

我正在尝试从我的 firebase 中读取所有数据 但我不能这样做

这是我的代码:

firebase.database().ref().child('users/').on('value',function(snapshot)                   {
  window.alert(snapshot.val());
});

我的火力基地看起来像这样:

"users":    
{

  "uid": {

    "username": "AAA",
    "email": "aaa@a.com"
  },

  "uid": {

    "username": "ZZZ",
    "email": "zzz@z.com"
  }

}

我无法使用此代码获取任何数据

更新

我已经在 firebase 中定义了我的安全性

{
 "rules":{

  "users":{

    "$uid": {
      // grants write access to the owner of this user account whose uid must exactly match the key ($uid)
      ".write": "auth !== null && auth.uid === $uid ",      
      //".write": "auth !== null && (auth.uid === $uid || auth.uid ==='0127a226-83ec-4eae-bbc7-53b81da10c0d')",

      // grants read access to any user who is logged in with an email and password
      ".read": "auth !== null &&  auth.uid ===$uid "
      //".read": "auth !== null &&  (auth.uid ===$uid  || auth.uid ==='0127a226-83ec-4eae-bbc7-53b81da10c0d')"

      //".validate": "newData.hasChildren(['full_name','uid','state_light'])"   
  }
}

} }

【问题讨论】:

  • 查看我的答案,如果不能解决问题,您的初始 firebase 参考是如何定义的?
  • 现在在 console.firebase.google.com 上创建的新 Firebase 数据库要求用户在读取数据之前进行身份验证。很可能您没有读取数据的权限。一种快速查看是否是原因的方法是,将错误处理程序添加到您的 on() 调用中:on('value', ..., function(error) { console.error(error); } 并检查浏览器的 JavaScript 控制台。
  • 它显示错误,我在线程中发布我的安全性
  • 您可以删除您的规则并将主规则节点下的 .read 和 .write 都设置为 true 吗?这将验证规则是否导致问题。
  • 我认为不是规则问题,因为我有应用程序使用相同的规则,但它可以正确读/写

标签: javascript firebase firebase-realtime-database


【解决方案1】:

从 .child('users/') 中删除多余的 / 并将其格式化

firebase.database().ref('users').on('value', function(snapshot) {
  window.alert(snapshot.val());
});

此外,在您的 Firebase 结构中,您不能有两个具有相同名称 (uid) 的键。

【讨论】:

  • 仍然无法读取数据哦,uid 只是我使用 firebase 教程定义 firebase 的示例,我添加了一个代码 var rootRef = firebase.database().ref();
猜你喜欢
  • 2017-10-04
  • 1970-01-01
  • 1970-01-01
  • 2018-06-30
  • 2021-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多