尝试将我的 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;
}
}
}
- 测试节点中的数据
最佳答案
您的 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/