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>
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With