Yako
Yako

Reputation: 3484

Using Axios within nuxtServerInit()

I need to get remote data to be displayed in every pages.

This call is perfomed in store/index.js:

export const state = () => ({
  contact: {
    hello: "World"
  }
});

export const actions = {
  async nuxtServerInit({ commit, state }) {
    const { contactData } = await this.$axios.get("/contact");
    commit("SET_CONTACT", contactData);
  }
};

export const mutations = {
  SET_CONTACT(state, contactData) {
    state.contact = contactData;
  }
};

Problem is that the value of contact turns to undefined in the store, whereas expected content is retrieved through Axios (the retrieved content is displayed in the SSR console...)

What am I missing here?

Upvotes: 0

Views: 933

Answers (1)

Vimbi
Vimbi

Reputation: 104

export const actions = {
  async nuxtServerInit({ commit, state }, {app} ) {
    const { contactData } = await app.$axios.get("/contact");
    commit("SET_CONTACT", contactData);
  }
};

Upvotes: 0

Related Questions