Nuxt.js: Why nuxtServerInit doesn't work with axios this case?

Created on 5 Feb 2018  路  3Comments  路  Source: nuxt/nuxt.js

I have in my actions.js

import axios from 'axios'

export default {
  nuxtServerInit ({ commit, state }, { app }) {
    axios.get(`http://api.example.com/articles/sections`)
      .then((res) => {
        commit('SET_SECTIONS', res.data)
      })
  }
}

But it doesn't work. I also tried @nuxt/axios module, same.

This question is available on Nuxt.js community (#c2402)

Most helpful comment

Maybe use a return before axios.get(...) or:

import axios from 'axios'

export default {
  async nuxtServerInit ({ commit, state }, { app }) {
    let res = await axios.get(`http://api.example.com/articles/sections`)
    commit('SET_SECTIONS', res.data)
  }
}

Async function calls must returns a Promise

All 3 comments

Maybe use a return before axios.get(...) or:

import axios from 'axios'

export default {
  async nuxtServerInit ({ commit, state }, { app }) {
    let res = await axios.get(`http://api.example.com/articles/sections`)
    commit('SET_SECTIONS', res.data)
  }
}

Async function calls must returns a Promise

@EllenFawkes yes, it works this way. I guess it's time to begin to use async/await in my code =) Thanks!

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

msudgh picture msudgh  路  3Comments

maicong picture maicong  路  3Comments

o-alexandrov picture o-alexandrov  路  3Comments

vadimsg picture vadimsg  路  3Comments

danieloprado picture danieloprado  路  3Comments