0

this is my code :

export const state = () => ({
  products: []
});

export const getters = {
  getProducts: state => {
    return state.products;
  }
};
export const mutations = {
  SET_IP: (state, payload) => {
    state.products = payload;
  }
};

export const actions = () => ({
  async getIP({ commit }) {
    const ip = await this.$axios.$get("http://localhost:8080/products");
    commit("SET_IP", ip);
  }
});

the server is working nicely but i just can't get the data into the store

1 Answers1

0

First of all, I highly recommend you rename your action and mutation to something like getProducts and SET_PRODUCTS instead of ip. Also make sure you change the variable name inside the action. While this doesn't change any functionality, it makes your code easier to read.

Second, maybe add a console.log(ip) right after you define the const in the action and see if you're getting the data you want in there. In most cases you're going to want to assign ip.data to your variable.

Lastly, make sure you're calling the action somewhere in the code. You should do it like this:

this.$store.dispatch('getIP'); // Using your current name
this.$store.dispatch('getProducts'); // Using my recommended name
Erik
  • 101
  • 1
  • 8