firebasevue.jsvuefire

unresolved variable $firebaseRefs vue js


I have an add function to add movies to firebase database

methods: {
            add() {
                this.$firebaseRefs.movies.push({
                    name: this.newItem.name,
                    price: this.newItem.genre,
                    rating: this.newItem.rating,
                    reviews: this.newItem.reviews,
                    cast: this.newItem.cast,

                });
                this.newItem.name = '';
                this.newItem.genre = '';
                this.newItem.rating = '';
                this.newItem.reviews= '';
                this.newItem.cast= '';
                this.$router.push('/dashboard')
            }
        }
    }

But am getting an error Unresolved variable $firebaseRefs and when I try to add the route changes to http://localhost:8080/add?

I have imported that

import { db } from '../db';

db.js

import * as firebase from "firebase";
import store from "./store";
let config={
    apiKey: "AIzaSyDQ2dXBMuIJ2EBYeVqucJpOF33C0tsFlLk",
    authDomain: "tv-show-tracker-9899e.firebaseapp.com",
    databaseURL: "https://tv-show-tracker-9899e.firebaseio.com",
    projectId: "tv-show-tracker-9899e",
    storageBucket: "tv-show-tracker-9899e.appspot.com",
    messagingSenderId: "433917891798",
    appId: "1:433917891798:web:bb35a74ae42e6db339a577"
};
let app = firebase.initializeApp(config);
export const db = app.database();

// eslint-disable-next-line no-unused-vars
let firebaseRefs = db.ref('movies');

firebase.auth().onAuthStateChanged(user => {
    store.dispatch("fetchUser", user);
});

Below is the main.js file

main.js

import Vue from 'vue'
import App from './App.vue'
import router from "./routes/route.js";

import store from "./store";



Vue.config.productionTip = false;




new Vue({
  router,
  store,
  render: h => h(App),
}).$mount('#app')

Solution

  • As explained in the vuefire documentation, you need to install Vuefire as a Vue plugin. Therefore, since you use the Realtime Database, you need to adapt your main.js file as follows

    import Vue from 'vue'
    import App from './App.vue'
    import router from "./routes/route.js";
    import store from "./store";
    import { rtdbPlugin } from 'vuefire'
    
    Vue.use(rtdbPlugin)
    Vue.config.productionTip = false;
    
    new Vue({
      router,
      store,
      render: h => h(App),
    }).$mount('#app')
    

    Do not forget to install the latest version of Vuefire as follows (see doc):

    npm install vuefire