【问题标题】:Cannot access firebase database (despite making read rule true)无法访问 firebase 数据库(尽管使读取规则为真)
【发布时间】:2017-07-18 16:20:12
【问题描述】:

我正在使用 React-Native 和 Firebase 构建一个应用程序。

在另一个应用程序中运行的代码(从 firebase 中提取)这次无法运行。因此,我将其修改为更简单 - 一次数据调用,我收到以下错误:

Possible Unhandled Promise Rejection (id: 0):
Error: permission_denied at /userList: Client doesn't have permission to access the desired data.

即使我将规则设为“真实”(对任何人开放),我仍然会遇到同样的错误。在同一个应用程序的其他地方,我还有其他可用的读/写功能。

这是我当前的规则和数据库结构:

{
  "rules": {
    "users": {
      "$uid": {
        ".read": "$uid === auth.uid",
        ".write": "$uid === auth.uid"
      }
    },
    "public": {
      "$uid": {
        ".read": "auth != null",
        ".write": "auth != null"
      }
    },
    "feedback": {
       "$uid": {
        ".read": "auth != null",
        ".write": "auth != null"
      }
    },
    "userList": {
     "$uid": {
      ".read": "auth != null",
      ".write": "auth != null"
    }
   }
  }
}

Database Structure

这是我发出调用的动作创建者:

import firebase from 'firebase';

export const NAMES_FETCH = 'names_fetch'
export const namesFetch = () => {

  return (dispatch) => {
      firebase.database().ref(`/userList`).once('value').then(function(snapshot) {
        var names = snapshot.val();
        console.log('FIREBASE PRINT', names)
      });

    };
  };

我已经尝试了所有方法,但我束手无策。谢谢!

【问题讨论】:

  • 忘了提到如果我将 ref 更改为 'users' 而不是 'userList' 则相同的代码可以工作

标签: javascript firebase firebase-realtime-database firebase-authentication firebase-security


【解决方案1】:

当您附加侦听器时,Firebase 会检查读取权限。如果您对该位置具有读取权限,则允许读取或查询。如果您对该位置没有读取权限,则读取或查询将被拒绝。

在您的情况下,读取被拒绝,因为没有人拥有对/userList 的读取权限。

所以:要查询一个位置,您必须对该位置具有读取权限。而且,一旦您对某个位置具有读取权限,您还可以读取该位置下的所有数据 (docs。这意味着查询不能用于过滤您无权访问的数据,Firebase 文档称之为 @987654322 @。

另请参阅许多其他 questions about this topic 中的一些,其中许多包含此行为的解决方法。

【讨论】:

  • 感谢您的快速回答!只是一个后续:为什么没有人对 /userList 有读取权限?我认为这就是我的规则在 userList 下允许的。最简单的解决方法是授予所有读取权限,但是从您的回答中我猜这违反了规则不是过滤器指南???谢谢
  • 我明白了我的错误。谢谢你的回答!
猜你喜欢
  • 1970-01-01
  • 2023-03-25
  • 2018-12-15
  • 1970-01-01
  • 2017-09-28
  • 2018-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多