javascript - JS Firebase Database Error db.ref is not a function - Stack Overflow

Hi I am trying to implement firebase realtime database API in my website, I am following this documenta

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

this is my code:

 <script type="module">
    // Import the functions you need from the SDKs you need
    import { initializeApp } from ".8.3/firebase-app.js";
    import { getAnalytics } from ".8.3/firebase-analytics.js";
    import { getDatabase } from ".8.3/firebase-database.js";;

    // 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: "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?

Hi I am trying to implement firebase realtime database API in my website, I am following this documentation: https://firebase.google./docs/database/admin/save-data#node.js but I get this error:

this is my code:

 <script type="module">
    // Import the functions you need from the SDKs you need
    import { initializeApp } from "https://www.gstatic./firebasejs/9.8.3/firebase-app.js";
    import { getAnalytics } from "https://www.gstatic./firebasejs/9.8.3/firebase-analytics.js";
    import { getDatabase } from "https://www.gstatic./firebasejs/9.8.3/firebase-database.js";;

    // TODO: Add SDKs for Firebase products that you want to use
    // https://firebase.google./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?

Share Improve this question asked Jun 13, 2022 at 16:08 ContaConta 1761 gold badge7 silver badges22 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

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:

Add "ref" to the imports from firebase/database:

import { getDatabase, ref } 
  from "<wherever you are getting firebase/database from>";

And replacing your last line:

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

Since you seem to be taking outdated code, I remend keeping the documentation handy (for example, this section on getting a reference) to pare the v8 and v9 code samples, as well as reading the upgrade guide.

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.

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742327006a4422952.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信