Skip to content
Advertisement

How do I get database data using firebase/nuxtjs

I don’t know how to get the data via

this.$fire.database

I’ve read the firebase docs but in firebase/nuxtJs different it becomes easier https://firebase.nuxtjs.org/

In firebase docs

import { getDatabase, ref, onValue} from "firebase/database";

const db = getDatabase();
const starCountRef = ref(db, 'posts/' + postId + '/starCount');
onValue(starCountRef, (snapshot) => {
  const data = snapshot.val();
  updateStarCount(postElement, data);
});


But when I call onValue, it doesn’t find it

this.$fire.database.onValue()

nuxt.config.js file

  modules: [
    // https://go.nuxtjs.dev/axios
    '@nuxtjs/axios',
    [
      '@nuxtjs/firebase',
      {
        config: {
          apiKey: "<private data>",
          authDomain: "<private data>",
          projectId: "<private data>",
          storageBucket: "<private data>",
          messagingSenderId: "<private data>",
          appId: "<private data>"
        },
        services: {
          auth: true, // Just as example. Can be any other service.
          database: {
            emulatorPort: 3000,
            emulatorHost: 'localhost',
          }
        },
      }
    ]
  ],

Answer

Simply

this.$fire.database.ref('name').on("value", function(e) {
  console.log(e.val())
})

simple explanation :

ref("name")

in Realtime Database : enter image description here

Whenever the value is changed, the function will run

Advertisement