0

I am new to Vue.

I am getting data from an external API using Axios:

import axios from 'axios'
var headers = {
    'Content-Type': 'application/json'
}
const api = 'https://avoindata.prh.fi/tr/v1/3132320-8'

methods: {
  onSubmit: function (event) {
  axios.get(api, headers)
    .then((response) => {
      this.maindata = response.data
      console.log(JSON.stringify(response.data))
    })
    .catch(e => {
      this.errors.push(e)
    })
}},

Once I get a response from Axios:

.then((response) => {
      this.maindata = response.data

I can only read data from maindata (array):

<ul >
    <li> Type: {{ maindata.type }}</li>
    <li> Version: {{ maindata.version }}</li>
</ul>

But not array results below:

<ul v-for="(results, index) in maindata" v-bind:key="index">
  <li> BusinessID: {{ results.businessId }}</li>
</ul> 

Using the following:

data () {
    return {
        maindata: [
        {
           type: '',
           version: '',
           results: [{
               businessId: '',
               name: ''
           }]
       }]
    …
    }
}

The JSON looks like this:

{
"type": "fi.prh.opendata.tr", 
"results":
    [
        { 
            "businessId":"3132320-8",
            "name":"Uracom 
        }
    ]
}

Question: How do I extract data from the results array?

Thanks MikroMike.

1
  • Have you tried v-for="(results,index) in maindata.results" ? Commented Jun 10, 2020 at 11:43

1 Answer 1

1

your v-for is wrong, try this:

<ul v-for ="(result, index) in maindata.results"
  v-bind:key="index">
 <li> BusinessID: {{ result.businessId }}</li>
 </ul> 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.