Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"firebase.database.ref is not a function" when connecting to Firebase using JS [duplicate]

I am getting a weird error when trying to connect a web page to firebase realtime database. I get the error,

Uncaught TypeError: firebase.database.ref is not a function.

The error points to the below line of code

const db = firebase.database.ref();

I copied the script from my firebase project and I added the imports. For the initialisation I have removed the apiKey etc for the question.

<script src="https://www.gstatic.com/firebasejs/5.10.0/firebase-app.js"> 
</script>
<script src="https://www.gstatic.com/firebasejs/5.10.0/firebase- 
database.js"></script>

<script>
// Initialize Firebase
const config = {
          apiKey: "",
          authDomain: "",
          databaseURL: "",
          projectId: "",
          storageBucket: "",
          messagingSenderId: ""
    };

    firebase.initializeApp(config);
</script>

<script>
    const db = firebase.database.ref();
</script>
like image 256
user10431501 Avatar asked Jan 02 '23 01:01

user10431501


1 Answers

You have to do

const db = firebase.database().ref();

See https://firebase.google.com/docs/database/web/start and https://firebase.google.com/docs/reference/js/firebase.database.Database


Note that you could maybe rename your variable to rootRef instead of db, and use db for firebase.database()

like image 171
Renaud Tarnec Avatar answered Jan 08 '23 02:01

Renaud Tarnec