Nuxt.js: method does not returns data.

Created on 26 May 2017  路  6Comments  路  Source: nuxt/nuxt.js

<script>

import axios from 'axios'

export default {
    data () {
        return {
            firstRecord: 1,
            count: 6,
            searchResult: this.loadSearchResult()
        }
    },
    methods: {
        loadSearchResult () {
            return axios.get(store.state.serviceUrl+ 'boxes?q=' + this.$route.query.stext + '&firstRecord=' + 1 + '&count=' + 9)
            .then((response) => {
                return response.data.response.data
            })
        }
    }
}
</script>

Why loadSearchResult returns empty to caller?

This question is available on Nuxt.js community (#c675)
help-wanted

Most helpful comment

Maybe asyncData would be of use here then ?

...
async asyncData () {
  const { data } = await axios.get(`store.state.serviceUrl+ 'boxes?q=' + this.$route.query.stext + '&firstRecord=' + 1 + '&count=' + 9`)
  return { 
    searchResult: data.response.data
  }
}
...

All 6 comments

...
.then((response) => {
    return response.data.response.data
})
...

Maybe is thisundefined ? axios' data is only in the response.data.

If not, the uri seems a bit off

store.state.serviceUrl+ 'boxes?q=' + this.$route.query.stext + '&firstRecord=' + 1 + '&count=' + 9

Shouldn't this be

this.$store.state.serviceUrl+ 'boxes?q=' + this.$route.query.stext + '&firstRecord=' + 1 + '&count=' + 9

@Kylart i did console.log it outputs data.

Maybe asyncData would be of use here then ?

...
async asyncData () {
  const { data } = await axios.get(`store.state.serviceUrl+ 'boxes?q=' + this.$route.query.stext + '&firstRecord=' + 1 + '&count=' + 9`)
  return { 
    searchResult: data.response.data
  }
}
...

axios module for nuxt is now ready. You may give it a try ;)

As say @Kylart for this case is not necessary axios, only you should to use searchResult: this.$store.state.....

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

o-alexandrov picture o-alexandrov  路  3Comments

mattdharmon picture mattdharmon  路  3Comments

pehbehbeh picture pehbehbeh  路  3Comments

jaredreich picture jaredreich  路  3Comments

gary149 picture gary149  路  3Comments