Home

Issue: 1900

Setting meta data based on asyncData

[issue link]

In my SPA i am dealing with a slightly unusual problem. I am fetching data for each page from static json files. Within these files all the content is represented, including the meta data. Unfortunately i am experiencing that the variable can be null due to its async behaviour and cause an error. How do i work around this?

Here is an example of what i am doing:

export default {
  data () {
    return {
      content: ''
    }
  },
  async asyncData ({req, params}) {
    let { data } = await axios.get(`/data/blog/${params.slug}.json`)
    return { content: data }
  },
  head () {
    if (this.content.seoMetaTags) {
      this.content.seoMetaTags.map((meta) => {
        if (meta.tagName === 'title') {
          return {
            title: 'lol'
          }
        }
      })
    }
  }
}
This question is available on Nuxt.js community (#c1700)