Skip to content

JS Firebase Database Error db.ref is not a function

Hi I am trying to implement firebase realtime database API in my website, I am following this documentation: but I get this error:

enter image description here

this is my code:

 <script type="module">
    // Import the functions you need from the SDKs you need
    import { initializeApp } from "";
    import { getAnalytics } from "";
    import { getDatabase } from "";;

    // TODO: Add SDKs for Firebase products that you want to use

    // Your web app's Firebase configuration
    // For Firebase JS SDK v7.20.0 and later, measurementId is optional
    const firebaseConfig = {
      apiKey: "xxxxxxxxxxxxxxxxxxxxx",
      authDomain: "",
      databaseURL: "xxxxxxxxxxxxxxxx",
      projectId: "xxxxx",
      storageBucket: "xxxxxxxxx",
      messagingSenderId: "xxxx",
      appId: "xxxxx",
      measurementId: "xxxxxxx"

    const fireapp = initializeApp(firebaseConfig);
    const db = getDatabase(fireapp);
    const ref = db.ref('server/saving-data/fireblog');

What am I doing wrong? Could the version I am using be incorrect?


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:

const dbref = ref(db, 'server/saving-data/fireblog');

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.