Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with async data in pages template #690

Closed
alexmoreno opened this issue May 9, 2017 · 3 comments
Closed

Problems with async data in pages template #690

alexmoreno opened this issue May 9, 2017 · 3 comments
Labels

Comments

@alexmoreno
Copy link

alexmoreno commented May 9, 2017

I'm at /loja/pages/index.vue
This is my code:

<template>
	<div>
	  <template-home></template-home>
	  {{produtos}}
    </div>
</template>

<script>
  import TemplateHome from '~/components/default/Templates/Home'
  import {$get} from '~/.nuxt-helpers/axios';
  import axios from 'axios'

  export default {
    components: {
      TemplateHome
    },
    async asyncData({params}) {
        let { produtos } = await axios.get('http://localhost:3010/api/produtos');
        return {produtos}
    }
     
    
  }
</script>

OK, I get that I cannot use asyncData into layouts or components, but why it keeps returning me [Vue warn]: Property or method "produtos" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option. ?
I'm already using nuxtServerInit to get some initial data to my state, but I think the right way to pass data to components is by props (I might be wrong here).
What I'm doing wrong here?

This question is available on Nuxt.js community (#c593)
@alexmoreno
Copy link
Author

With promises it worked.

    asyncData({params}) {
        return  axios.get('http://localhost:3010/api/produtos')
        .then((res) => {
        	return {produtos: res.data}
        })
    }

Why? haha

@stephan281094
Copy link

stephan281094 commented May 9, 2017

It seems like you forgot to about the data property in the async/ await example. Instead you could do something like:

async asyncData ({ params }) {
  const { data } = await axios.get('http://localhost:3010/api/produtos')
  return { produtos: data }
}

@Atinux Atinux closed this as completed May 9, 2017
@lock
Copy link

lock bot commented Nov 5, 2018

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.

@lock lock bot locked as resolved and limited conversation to collaborators Nov 5, 2018
@danielroe danielroe added the 2.x label Jan 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants