0

I am trying to read and write on firebase database from web application and i got this error the error appear when the writeUserData() function called the error appear when the writeUserData() function called

and this is the js code

<script src="https://www.gstatic.com/firebasejs/5.5.1/firebase.js"></script>
<script> // Initialize Firebase var config = { // copied from firebase website }; firebase.initializeApp(config); writeUserData(); function writeUserData() { firebase.database().ref('options/gK6YJVMAr82Pp8GHmjJa').set({ active_table: '2222', }); } </script>

and this is the firebase database rules this is the rules

And this is the database structure this is the data structure

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
Ammar Qasem
  • 33
  • 1
  • 2
  • 6

1 Answers1

1

You are mixing-up the two database types offered by Firebase: The Real Time Database and Firestore.

You are storing your data (and writing your security rules) in Firestore but the code you are using for writing data (firebase.database().ref('options/gK6YJVMAr82Pp8GHmjJa').set()) corresponds to the Real Time Database.

You will find here the documentation to write data to Firestore: https://firebase.google.com/docs/firestore/manage-data/add-data

So in your case you should do something along theses lines:

var db = firebase.firestore();
db.collection("options").doc("gK6YJVMAr82Pp8GHmjJa").set({
            active_table: '2222',
        })
.then(function() {
    console.log("Document successfully written!");
})
.catch(function(error) {
    console.error("Error writing document: ", error);
});

Note the use of firebase.firestore() instead of firebase.database().

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121