Hi I am trying to implement firebase realtime database API in my website, I am following this documentation: https://firebase.google.com/docs/database/admin/save-data#node.js but I get this error:
this is my code:
JavaScript
x
27
27
1
<script type="module">
2
// Import the functions you need from the SDKs you need
3
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.8.3/firebase-app.js";
4
import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.8.3/firebase-analytics.js";
5
import { getDatabase } from "https://www.gstatic.com/firebasejs/9.8.3/firebase-database.js";;
6
7
// TODO: Add SDKs for Firebase products that you want to use
8
// https://firebase.google.com/docs/web/setup#available-libraries
9
10
// Your web app's Firebase configuration
11
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
12
const firebaseConfig = {
13
apiKey: "xxxxxxxxxxxxxxxxxxxxx",
14
authDomain: "xx.x.xxx",
15
databaseURL: "xxxxxxxxxxxxxxxx",
16
projectId: "xxxxx",
17
storageBucket: "xxxxxxxxx",
18
messagingSenderId: "xxxx",
19
appId: "xxxxx",
20
measurementId: "xxxxxxx"
21
};
22
23
const fireapp = initializeApp(firebaseConfig);
24
const db = getDatabase(fireapp);
25
const ref = db.ref('server/saving-data/fireblog');
26
</script>
27
What am I doing wrong? Could the version I am using be incorrect?
Advertisement
Answer
You’re mixing the new modular/v9 syntax of the API with the older namespaced syntax, and that won’t work.
In v9 the equivalent of that last line is:
JavaScript
1
2
1
const dbref = ref(db, 'server/saving-data/fireblog');
2
Since you seem to be taking outdated code, I recommend keeping the documentation handy (for example, this section on getting a reference) to compare the v8 and v9 code samples, as well as reading the upgrade guide.