0

I am trying to display the borders of a country from restcountries api (https://restcountries.eu/) as clickable buttons.

this is how I try to build the url for the api

    mounted() {

        axios
        .get(this.urlDetail)
        .then(response => (
            this.details = response.data[0]
        ))
        
        this.borders = this.details.borders.join(";");
        this.urlBorders = "https://restcountries.eu/rest/v2/alpha?codes=" + this.borders;


        fetch(this.urlBorders)
            .then(res => res.json())
            .then(data => this.bordersData = data)

the problem is, that the details array is empty at that moment. When I reload the page, the data is fetched correctly.

I tried to:

  • use beforeMount()
  • use a isFetching boolean
  • get the data with @click-function
  • tried is with these function in mounted():
        document.onreadystatechange = () => {
            if (document.readyState == "complete") {
              console.log('Page completed with image and files!')
            }
        }

this is my data:

    data() {
        return {
            isFetching: true,
            urlDetail: 'https://restcountries.eu/rest/v2/name/' + this.$route.params.countryName,
            details: [],
            borders: "",
            urlBorders: "",
            bordersData: []
        }

this is the relevant html snipped to display the buttons:

<p><strong>Border Countries:</strong><button class="border-button" v-for="border in bordersData"><router-link :to="{ name: 'border', params: {borderName: border.name}}">{{ border.name }}</router-link></button></p>

thanks for helping!

1 Answer 1

1

Try to wait for responses:

methods: {
  async getBorders() {
    await axios
      .get(this.urlDetail)
      .then((response) => (this.details = response.data[0]));
  },

  setBorders() {
    this.borders = this.details.borders.join(";");
    this.urlBorders =
      "https://restcountries.eu/rest/v2/alpha?codes=" + this.borders;
  },

  async getDets() {
    await axios
      .get(this.urlBorders)
      .then((response) => (this.bordersData = response.data));
    },
  },
},

async mounted() {
  await this.getBorders();
  this.setBorders();
  await this.getDets();
},
Sign up to request clarification or add additional context in comments.

1 Comment

thanks that did the trick! just one little note if someone uses this solution: in the getDets method it should be: .then((response) => (this.bordersData = response.data)); (.data was missing) to work properly

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.