javascript - Firebase 未与 React 应用程序连接 - 权限被拒绝 - 在线检查了所有关于此无解决方案的问题

标签 javascript reactjs firebase react-native

尝试将我的 React 应用程序与 Firebase 连接时出现错误。

Uncaught (in promise) Error: permission_denied at /tests: Client doesn't have permission to access the desired data.

我已经阅读了所有的答案,一切似乎都是正确的,请任何人看看哪里出了问题。

当我在模拟器中测试规则时 模拟器 type = get simulator location = database/tests 它显示成功输出。

我可能没有正确使用 ref

React 应用内部 - 我正在使用的包 - "firebase": "^5.4.2",

react 代码
1.Config.js文件

// Initialize Firebase
import firebase from 'firebase';
const config = {
    apiKey: "xxxx",
    authDomain: "xxxx",
    databaseURL: "xxxx",
    projectId: "xxx",
    storageBucket: "xxx",
    messagingSenderId: "xxx"
};
const fire = firebase.initializeApp(config)
export { fire }

2。 在我的容器组件中

import { fire } from '../../Config/Config';

componentWillMount() {

   let testRef = fire.database().ref().child('tests');

   //also tried this
   //let testRef = fire.database().ref('tests');

   console.log('testRef', testRef)
   testRef.once("value").then((f) => {
   console.log(f);
  });
 }

3。 我的 firebase 帐户中的规则

service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
           allow read, write: if true;
        }
      }
    }
  1. 测试节点中的数据

Image representing how node is present in my databse

最佳答案

您的 firebase 规则建议您使用 Firestore database但您的查询是为从 Realtime database 中获取数据而编写的

所以你的 testRef 应该是这样的:

var testRef = fire.firestore().collection("tests");

您可以像这样从中获取所有文档:

testRef
.get()
.then(function(querySnapshot) {
    querySnapshot.forEach(function(doc) {
        console.log(doc.id, " => ", doc.data());
    });
})
.catch(function(error) {
    console.log("Error getting documents: ", error);
});

您还应该考虑使用 react-native-firebase对于你的 react-native 项目,因为在我看来你正在使用网络库

关于javascript - Firebase 未与 React 应用程序连接 - 权限被拒绝 - 在线检查了所有关于此无解决方案的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52134245/

相关文章:

firebase - .onAuthStateChanged 方法是否计入 Firebase 身份验证验证?

ios - 重置 Firebase 缓存

javascript - 如何在另一个函数中使用异步函数?

reactjs - React 组件未呈现为 index.html

javascript - 如何从 angular2 项目访问 "$wakanda"服务以检索数据?

css - 在 React 中如何只导入需要的 Bootstrap 样式。 Bootstrap css 模块?

node.js - 无法在 devDependency 上找到文件

Firebase 云消息传递 - 向消息添加分析标签

javascript - 如何启用文件选择按钮?

javascript - Java中的Math.exp计算错误?