Reputation: 19
I have been searching for hours and i can't understand why i'm getting this error: [vuex] unknown mutation type: groceryStore/getStoreApple on all of the commits to the mutations in the groceryStore-module. As far as i can see i'm doing it by the book. Please help if you have any idea on what i'm doing wrong when i'm trying to reach my functions in the groceryStore-module.
This is my index.js: located in src/store/index.js
import Vue from "vue";
import Vuex from "vuex";
import * as groceryStore from "./modules/groceries";
Vue.use(Vuex);
export default new Vuex.Store({
modules: {
groceryStore: groceries,
}
});
here is the module i'm trying to access: src/store/modules/groceries.js
import { db } from "../../main";
const groceryStore = {
namespace: true,
state: {
fruit: {},
fish: {},
meat: {},
sauce: {},
spices: {},
vegetables: {},
bakery: {},
beverages: {},
},
mutations: {
getStoreApple(state) {
db.ref()
.child("groceries")
.child("Fruits")
.child("Apple")
.get()
.then((usersObject) => {
state.fruit = usersObject.val();
});
},
getStoreFish(state) {
db.ref()
.child("groceries")
.child("fish")
.child("salmon")
.get()
.then((usersObject) => {
state.fish = usersObject.val();
});
},
getStoreMeat(state) {
db.ref()
.child("groceries")
.child("meat")
.child("chicken")
.get()
.then((usersObject) => {
state.meat = usersObject.val();
});
},
getStoreSauce(state) {
db.ref()
.child("groceries")
.child("sauce")
.child("mustard")
.get()
.then((usersObject) => {
state.sauce = usersObject.val();
});
},
getStoreSpices(state) {
db.ref()
.child("groceries")
.child("spices")
.child("chillipowder")
.get()
.then((usersObject) => {
state.spices = usersObject.val();
});
},
getStoreVegetables(state) {
db.ref()
.child("groceries")
.child("vegtables")
.child("carrot")
.get()
.then((usersObject) => {
state.vegetables = usersObject.val();
});
},
getStoreBakery(state) {
db.ref()
.child("groceries")
.child("bakery")
.child("bread")
.get()
.then((usersObject) => {
state.bakery = usersObject.val();
});
},
getStoreBeverages(state) {
db.ref()
.child("groceries")
.child("beverages")
.child("juices")
.get()
.then((usersObject) => {
state.beverages = usersObject.val();
});
},
},
getters: {
appleGetter(state) {
return state.fruit;
},
fishGetter(state) {
return state.fish;
},
meatGetter(state) {
return state.meat;
},
sauceGetter(state) {
return state.sauce;
},
spicesGetter(state) {
return state.spices;
},
vegetablesGetter(state) {
return state.vegetables;
},
bakeryGetter(state) {
return state.bakery;
},
beveragesGetter(state) {
return state.beverages;
},
},
};
export default groceryStore;
And this is the component where i try to render the code: src/views/Home.vue
<template>
<div class="home">
<h1 v-if="route">This is a {{ route }} page</h1>
<div id="flexContainer">
<div
class="groceryNameContainer"
v-for="grocery in groceries"
:key="grocery.id"
>
<b-card
title=""
img-alt="Image"
img-top
tag="article"
style="max-width: 20rem;"
class="mb-2"
><b-card-img v-if="grocery.url" :src="grocery.url"></b-card-img>
<b-card-text>
{{ grocery.description }}
</b-card-text>
<b-card-text>
{{ grocery.price }}
</b-card-text>
<b-card-text>
{{ grocery.comparison }}
</b-card-text>
<b-button href="#" variant="primary">Add to Cart</b-button>
</b-card>
</div>
</div>
<router-view />
</div>
</template>
<script>
// import { db } from "../main";
export default {
data() {
return {
route: "",
};
},
computed: {
groceries: function() {
var groceriesArray = [];
groceriesArray.push(this.$store.getters["groceryStore/appleGetter"]);
groceriesArray.push(this.$store.getters["groceryStore/fishGetter"]);
groceriesArray.push(this.$store.getters["groceryStore/meatGetter"]);
groceriesArray.push(this.$store.getters["groceryStore/sauceGetter"]);
groceriesArray.push(this.$store.getters["groceryStore/spicesGetter"]);
groceriesArray.push(this.$store.getters["groceryStore/vegetablesGetter"]);
groceriesArray.push(this.$store.getters["groceryStore/bakeryGetter"]);
groceriesArray.push(this.$store.getters["groceryStore/beveragesGetter"]);
console.log(groceriesArray);
return groceriesArray;
},
},
created() {
this.$store.commit("groceryStore/getStoreApple");
this.$store.commit("groceryStore/getStoreFish");
this.$store.commit("groceryStore/getStoreMeat");
this.$store.commit("groceryStore/getStoreSauce");
this.$store.commit("groceryStore/getStoreSpices");
this.$store.commit("groceryStore/getStoreVegetables");
this.$store.commit("groceryStore/getStoreBakery");
this.$store.commit("groceryStore/getStoreBeverages");
this.route = this.$route.name;
},
};
</script>
<style>
.nestedGroceryInfoContainer {
border: 1px solid black;
margin: 10px;
padding: 15px;
border-radius: 5px;
}
.descriptionContainer {
font-weight: 500;
padding-top: 200;
color: rgb(6, 6, 43);
}
.nav-text-collapse {
background-color: rgb(0, 255, 13);
}
.nameContainer {
font-weight: bold;
text-decoration: underline;
}
.priceContainer {
position: absolute;
margin-top: 13%;
border: none;
font-weight: bold;
font-size: 30px;
}
.groceryNameContainer > * {
flex-basis: 45%;
}
#flexContainer {
justify-content: center;
display: flex;
flex-wrap: wrap;
}
.innerProductContainer {
margin-left: 28%;
justify-content: center;
display: flex;
flex-wrap: wrap;
margin-top: -65%;
width: 30%;
height: 46%;
}
.productImage {
margin-left: 55px;
height: 150px;
width: 160px;
}
.detailsContainer {
justify-content: center;
display: flex;
flex-wrap: wrap;
}
.comparisonContainer {
color: #2f2626;
}
</style>
Here's also the main.js file:
import App from "./App.vue";
import router from "./router";
import store from "./store";
import Vue from "vue";
Vue.config.productionTip = false;
new Vue({
router,
store,
render: (h) => h(App),
}).$mount("#app");
Upvotes: 2
Views: 1791
Reputation: 11
import * as groceryStore from "./modules/groceries";
will give you an object of all the export from the file.(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#import_an_entire_modules_contents)
In your case it will probably give you an object
{
default: {}// your groceries object
}
so please try
import groceryStore from "./modules/groceries";
Upvotes: 0
Reputation: 1229
After we exchanged a few comments, I did some digging and found a fairly simple Vuex Module example that I had built recently. I am posting the store, module, and component here.
Your application is obviously much more complex, but one thing I noticed is that in your Vuex store you coded your module import as
import * as groceryStore from "./modules/groceries";
whereas in my store I coded the module import as
import userModule from './modules/user-store-module.js'
I'm not a JavaScript modules expert, but figured I would point it out. Here are my source files:
EDIT: I added in namespacing, getters, mutations, and additional component functionality to use them.
/store/index.js
import Vue from 'vue'
import Vuex from 'vuex'
import userModule from './modules/user-store-module.js'
Vue.use(Vuex)
export default new Vuex.Store({
modules: {
userModule: userModule
}
})
/store/modules/user-store-module.js
const userModule = {
namespaced: true,
state: () => ({
users: [
{
id: 1,
name: 'User1',
gender: 'male'
},
{
id: 2,
name: 'User2',
gender: 'female'
},
{
id: 3,
name: 'User3',
gender: 'male'
},
{
id: 4,
name: 'User4',
gender: 'female'
},
]
}),
getters: {
getMaleUsers: state => {
return state.users.filter( user => user.gender === 'male')
},
getFemaleUsers: state => {
return state.users.filter( user => user.gender === 'female')
},
},
mutations: {
addUser(state, newGender) {
let nextId = state.users.length + 1;
state.users.push({
id: nextId,
name: 'User' + nextId,
gender: newGender
})
}
}
}
export default userModule;
VuexModule.vue
<template>
<div class="vuex-module">
<div class="row">
<div class="col-md-6">
<h4>Users</h4>
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>NAME</th>
<th>GENDER</th>
</tr>
</thead>
<tbody>
<tr v-for="user in users" :key="user.id">
<td>{{ user.id }}</td>
<td>{{ user.name }}</td>
<td>{{ user.gender }}</td>
</tr>
</tbody>
</table>
<button type="button" class="btn btn-secondary" @click="addUser">Add Random User</button>
</div>
<div class="col-md-6">
<div class="row">
<div class="col-md-12">
<h4>Male Users</h4>
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>NAME</th>
<th>GENDER</th>
</tr>
</thead>
<tbody>
<tr v-for="user in maleUsers" :key="user.id">
<td>{{ user.id }}</td>
<td>{{ user.name }}</td>
<td>{{ user.gender }}</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="row">
<div class="col-md-12">
<h4>Female Users</h4>
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>NAME</th>
<th>GENDER</th>
</tr>
</thead>
<tbody>
<tr v-for="user in femaleUsers" :key="user.id">
<td>{{ user.id }}</td>
<td>{{ user.name }}</td>
<td>{{ user.gender }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
gender: 'male'
}
},
computed: {
users() {
return this.$store.state.userModule.users;
},
maleUsers() {
return this.$store.getters['userModule/getMaleUsers'];
},
femaleUsers() {
return this.$store.getters['userModule/getFemaleUsers'];
}
},
methods: {
addUser() {
let nextGender = this.gender === 'male' ? 'female' : 'male';
this.gender = nextGender;
this.$store.commit('userModule/addUser', this.gender);
}
}
}
</script>
Upvotes: 0