Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

271
Visualizações
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: https://firebase.google.com/docs/database/admin/save-data#node.js 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 "https://www.gstatic.com/firebasejs/9.8.3/firebase-app.js";
    import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.8.3/firebase-analytics.js";
    import { getDatabase } from "https://www.gstatic.com/firebasejs/9.8.3/firebase-database.js";;

    // TODO: Add SDKs for Firebase products that you want to use
    // https://firebase.google.com/docs/web/setup#available-libraries

    // Your web app's Firebase configuration
    // For Firebase JS SDK v7.20.0 and later, measurementId is optional
    const firebaseConfig = {
      apiKey: "xxxxxxxxxxxxxxxxxxxxx",
      authDomain: "xx.x.xxx",
      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');
  </script>

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

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

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.

about 4 years ago · Santiago Gelvez Relatório

0

You are using the Firebase Client SDK but referring to the documentation of Firebase Admin SDK that is not totally modular yet. Try refactoring the code using ref() function:

import { ref } from "firebase/database"

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

Checkout the documentation of Firebase JS SDK (the Modular tab) for correct syntax.

about 4 years ago · Santiago Gelvez Relatório

0

it seems like you're using the documentation for the NodeJS backend SDK. Your starting point for frontend developing should be firebase docs – web start.

For v9 of the Web-SDK, that you included into your website, there different functions to use. Check out firebase docs – web read/write for using them.

I was confused as well when I first used firebase for web developing some weeks ago. I hope it helps.

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda