2

I am making dynamic dropdown lists with Vue.JS , the Countries and Regions are fetching dynamically but the Cities JSON is received but not fetched into the view and there is no error shown in the console, I couldn't find my mistake.

HTML

<div id="app">
  <form action="">
    <div class="form-group">
      <label for="country"></label>
      <select v-model="country" name="country" class="form-control" @change="loadRegions">
        <option>Select country</option>
        <option v-for="country in countries" :value="country.id">@{{ country.name }}</option>
      </select>
    </div>

    <div class="form-group">
      <label for="region"></label>
        <select v-model="region" name="region" class="form-control" @change="loadCities">
          <option>Select region</option>
          <option v-for="region in regions" :value="region.id">@{{ region.name }}</option>
        </select>
    </div>

    <div class="form-group">
      <label for="city"></label>
        <select v-model="city" name="city" class="form-control">
          <option>Select city</option>
          <option v-for="city in cities" :value="city.id">@{{ city.name }}</option>
       </select>
     </div>
   </form>
 </div>

JS

 <script type="text/javascript">

  var app = new Vue({
    el: '#app',
     data() {
      return {
       countries: [],
       regions: [],
       cities: [],
       country:'',
       region:'',
       city:'',
   }
 },

 mounted() {
    this.loadCountries();
  },

   methods: {
    loadCountries() {
       axios.get('/countries')
       .then(response => this.countries = response.data.countries)
       .catch(error => console.log(error))
    },    
    loadRegions() {
       axios.get('/regions', {params: {country: this.country}})
       .then(response => this.regions = response.data.regions)
       .catch(error => console.log(error))
    },
    loadCities() {
       axios.get('/cities', {params: {country: this.country, region: this.region}})
       .then(response => this.cities = response.data.cities)
       .catch(error => console.log(error))
    }
   }
 })
 </script>

Json object in console

Devtools screenshot

20
  • there would be any error in axios. check response ? Commented Aug 11, 2018 at 2:06
  • Actually the (localhost:8000/cities?country=n&region=n) is returning objects but in the Vue Devtool it's returning empty array Commented Aug 11, 2018 at 3:03
  • no errors? in the controller? Commented Aug 11, 2018 at 5:54
  • I've had problems with axios passing multiple parameters, maybe it is related. I've stop using it since the guys there doesn't give a st about the community, things like this: github.com/axios/axios/issues/362#issuecomment-401014489 two years bug, no one there cares about... Commented Aug 11, 2018 at 12:40
  • 1
    Oh so there is the error... its in the axios. Maybe your params. Commented Aug 11, 2018 at 15:35

1 Answer 1

1

the mistake is on the last square brackets:

<option v-for="city in cities" :value="city.id">@{{ city.name }</option>

put another..

<option v-for="city in cities" :value="city.id">@{{ city.name }}</option>

Sign up to request clarification or add additional context in comments.

1 Comment

That mistake was just in the post , still nothing changed even with correct syntax

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.