My objective is to integrate a firestore collection with the Vuex state in order to utilize it across multiple pages. I attempted to follow this guide:
How to get collection from firestore and set to vuex state when the app is rendered?
After following the instructions in the post, I encountered an issue where either I overlooked something or the code provided was outdated. As a newcomer to Vuex, I may have made mistakes in the process without realizing.
store/index.js
import Vue from 'vue'
import Vuex from 'vuex'
const db = require('../components/fbInit')
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
categories: []
},
actions: {
fetchCategories({ commit }) {
db.collection('one').get().then(querySnapshot => {
if (querySnapshot.empty) {
// eslint-disable-next-line no-console
console.log('cannot find')
//this.$router.push('/HelloWorld')
} else {
this.loading = false;
var categories = [];
querySnapshot.forEach(doc => {
categories.push(doc.data());
});
commit("setCategories", categories);
}
});
}
},
mutations: {
setCategories(state, val) {
state.categories = val;
}
}
});
store.dispatch("fetchCategories");
export default store;
One.vue
<template>
<div class="flex-row justify-center ma-12">
<ul>
<li v-for="category in categories" :key="category.name">{{category.name}}</li>
</ul>
</div>
</template>
<script>
import { mapActions } from "vuex";
import { mapGetters } from "vuex";
// eslint-disable-next-line no-unused-vars
export default {
computed: {
categories() {
return this.$store.state.categories;
},
...mapGetters([])
},
methods: {
...mapActions(["fetchCatagories"])
}
};
</script>
Although I successfully connected to firestore and displayed its contents, I encountered the following error: Uncaught TypeError: db.collection is not a function.
I have not been able to load my firestore collection into the Vuex store state as desired. Any assistance on resolving this issue would be highly appreciated, especially since I am still learning how to use Vuex.
TLDR; Objective: Retrieve firestore collection ('One'), Save it in Vuex state, Utilize Vuex store to access the data across multiple pages without redundant calls.