Sandeep Chikhale
Sandeep Chikhale

Reputation: 1516

Firebase not connecting with React Application - Permission denied - Checked all question online on this no solution

Trying to connect my react application with Firebase getting the error.

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

I have read all the answers and everything seems correct an anybody please take a look at whats going wrong.

When I test rule in the simulator with simulator type = get simulator location = database/tests it is showing me success output.

May I am not using ref properly

Inside React app - Package I am using - "firebase": "^5.4.2",

React code
1.Config.js File

// 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. In my container component

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. Rules in my firebase account

service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
           allow read, write: if true;
        }
      }
    }
  1. Data in tests node

Image representing how node is present in my databse

Upvotes: 1

Views: 1292

Answers (1)

needsleep
needsleep

Reputation: 2725

Your firebase rules suggest you use the Firestore database but your query is written for fetching data from Realtime database

So your testRef should be like so:

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

and you can get all documents from it like so:

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

You should also consider using react-native-firebase for your react-native project cause it seems to me you're using the web library

Upvotes: 1

Related Questions