Home

Issue: 2307

nuxtServerInit Not Working with localhost:3000

[issue link]

hello,

i’m using Adonuxt template (Adonis + Nuxt)
well,
i have created a simple Vuex State called counter,
this is index.js content:

import axios from '~/plugins/axios'

export const actions = {

  async increment ({ commit }) {
    await axios.get('http://localhost:3000/api/v1/counter')
    .then(response => {
      commit('increment', response.data)
    })
    .catch(e => {
      console.log(e)
    })
  },

  async nuxtServerInit ({commit}) {
    await axios.get('https://www.random.org/integers/?num=1&min=1&max=2&col=1&base=10&format=plain&rnd=new')
    .then(response => {
      commit('increment', response.data)
    })
    .catch(e => {
      console.log(e)
    })
  }

}

export const state = () => ({
  counter: 0
})

export const getters = {
  getCounter (state) {
    return state.counter
  }
}

export const mutations = {
  increment (state, payload) {
    state.counter += payload
  }
}

My Local API link return: 99 (link 1)
the Random API link return: random numbers 1 or 2 (link 2)

my problem when using nuxtServerInit with local link(1)
nuxtServerInit should init the counter with 99, but NO -_- i get default value :0

otherwise: when i leave the code as it is above: random numbers its work and init the counter with 1 or 2.

  1. is that related to https?
  2. how i can call local api to so that nuxtServerInit can use it with axios!!!

Notice: the increment Action works fine with Local API, when clicking the button that increments counter go correctly: adding +99

But with nuxtServerInit Not Working!!!

my axios.js code:

import axios from 'axios'

const instance = axios.create({
  baseURL: process.env.API_HOST + ':' + process.env.API_PORT,
  transformRequest: function (request) {
    return request
  }
})

export default instance

i have tried the promises, and i have followed some threads from googling, but nothing changed!
i have tried api/v1/counter also but nothing …

any help! and thank you 😃

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