Populating options for select using v-for VueJS after handling load data event










2















I have this VueJS application written using Typescript. I'm using Axios to get data from my database. This works well and the result I get is an array which is what I was expecting. When I do console.log on this array I can see that it's the correct result.



However, when I try to loop through this array to create the options for my select drop down I get a blank list. Why isn't the result coming up even though the variable I'm looping through is an array?



Edit: I've added a picture here showing the Axios result (response.data)



enter image description here



export default class EditRoute extends Vue 
result: any;
selectedRoute: string;
constructor()
super();
this.selectedRoute = "";
this.result = ;


loadData()
axios.get('http://localhost:8080/routes')
.then(response => (this.result = response.data));




<select name="routeSelect" v-model="selectedRoute">
<option v-for="routes in result" v-bind:key="routes.name" v-bind:value="routes.name"> routes.name </option>
</select>
<button @click="loadData">Load data</button>









share|improve this question



















  • 1





    i'm trying an example like yours and it works fine

    – Boussadjra Brahim
    Nov 15 '18 at 20:18











  • @BoussadjraBrahim Okay. I'll post a picture of the result I get from my Axios call maybe that's the problem then

    – M. H
    Nov 15 '18 at 20:20















2















I have this VueJS application written using Typescript. I'm using Axios to get data from my database. This works well and the result I get is an array which is what I was expecting. When I do console.log on this array I can see that it's the correct result.



However, when I try to loop through this array to create the options for my select drop down I get a blank list. Why isn't the result coming up even though the variable I'm looping through is an array?



Edit: I've added a picture here showing the Axios result (response.data)



enter image description here



export default class EditRoute extends Vue 
result: any;
selectedRoute: string;
constructor()
super();
this.selectedRoute = "";
this.result = ;


loadData()
axios.get('http://localhost:8080/routes')
.then(response => (this.result = response.data));




<select name="routeSelect" v-model="selectedRoute">
<option v-for="routes in result" v-bind:key="routes.name" v-bind:value="routes.name"> routes.name </option>
</select>
<button @click="loadData">Load data</button>









share|improve this question



















  • 1





    i'm trying an example like yours and it works fine

    – Boussadjra Brahim
    Nov 15 '18 at 20:18











  • @BoussadjraBrahim Okay. I'll post a picture of the result I get from my Axios call maybe that's the problem then

    – M. H
    Nov 15 '18 at 20:20













2












2








2








I have this VueJS application written using Typescript. I'm using Axios to get data from my database. This works well and the result I get is an array which is what I was expecting. When I do console.log on this array I can see that it's the correct result.



However, when I try to loop through this array to create the options for my select drop down I get a blank list. Why isn't the result coming up even though the variable I'm looping through is an array?



Edit: I've added a picture here showing the Axios result (response.data)



enter image description here



export default class EditRoute extends Vue 
result: any;
selectedRoute: string;
constructor()
super();
this.selectedRoute = "";
this.result = ;


loadData()
axios.get('http://localhost:8080/routes')
.then(response => (this.result = response.data));




<select name="routeSelect" v-model="selectedRoute">
<option v-for="routes in result" v-bind:key="routes.name" v-bind:value="routes.name"> routes.name </option>
</select>
<button @click="loadData">Load data</button>









share|improve this question
















I have this VueJS application written using Typescript. I'm using Axios to get data from my database. This works well and the result I get is an array which is what I was expecting. When I do console.log on this array I can see that it's the correct result.



However, when I try to loop through this array to create the options for my select drop down I get a blank list. Why isn't the result coming up even though the variable I'm looping through is an array?



Edit: I've added a picture here showing the Axios result (response.data)



enter image description here



export default class EditRoute extends Vue 
result: any;
selectedRoute: string;
constructor()
super();
this.selectedRoute = "";
this.result = ;


loadData()
axios.get('http://localhost:8080/routes')
.then(response => (this.result = response.data));




<select name="routeSelect" v-model="selectedRoute">
<option v-for="routes in result" v-bind:key="routes.name" v-bind:value="routes.name"> routes.name </option>
</select>
<button @click="loadData">Load data</button>






html node.js typescript vue.js axios






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 20:34









Boussadjra Brahim

8,2143733




8,2143733










asked Nov 15 '18 at 20:09









M. HM. H

1047




1047







  • 1





    i'm trying an example like yours and it works fine

    – Boussadjra Brahim
    Nov 15 '18 at 20:18











  • @BoussadjraBrahim Okay. I'll post a picture of the result I get from my Axios call maybe that's the problem then

    – M. H
    Nov 15 '18 at 20:20












  • 1





    i'm trying an example like yours and it works fine

    – Boussadjra Brahim
    Nov 15 '18 at 20:18











  • @BoussadjraBrahim Okay. I'll post a picture of the result I get from my Axios call maybe that's the problem then

    – M. H
    Nov 15 '18 at 20:20







1




1





i'm trying an example like yours and it works fine

– Boussadjra Brahim
Nov 15 '18 at 20:18





i'm trying an example like yours and it works fine

– Boussadjra Brahim
Nov 15 '18 at 20:18













@BoussadjraBrahim Okay. I'll post a picture of the result I get from my Axios call maybe that's the problem then

– M. H
Nov 15 '18 at 20:20





@BoussadjraBrahim Okay. I'll post a picture of the result I get from my Axios call maybe that's the problem then

– M. H
Nov 15 '18 at 20:20












1 Answer
1






active

oldest

votes


















2














Since your result is an object which is containing one item, that item is an array called routes in this case you should loop through result.routes like so :



 <option v-for="routes in result.routes" v-bind:key="routes.name" v-bind:value="routes.name"> routes.name </option>


Extra Example :






new Vue(
el: '#app',
data: function()
return
selectedUser: '',
users: ,


,
mounted: function()
// this.loadData();
// this.timer = setInterval(this.loadData, 4000);
,

methods:
loadData: function()
axios.get('https://jsonplaceholder.typicode.com/users').then(response =>
this.users = response.data;

).catch(e =>

)



)

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
<meta charset="utf-8">
<title></title>
<script src="https://unpkg.com/vue@2.5.17/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://unpkg.com/vue-axios@2.1.4/dist/vue-axios.min.js"></script>
</head>

<body>
<div id="app">
<select name="userSelect" v-model="selectedUser">
<option v-for="user in users" v-bind:key="user.id" v-bind:value="user.name"> user.name </option>
</select>
<button @click="loadData">Load data</button>
</div>
</body>

</html>








share|improve this answer


















  • 1





    I tried it now it works. It makes sense too, I didn't think that it was an object and not an array. Thank you so much!

    – M. H
    Nov 15 '18 at 20:29






  • 1





    you're welcome i added also a working example for futur askers

    – Boussadjra Brahim
    Nov 15 '18 at 20:30











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327188%2fpopulating-options-for-select-using-v-for-vuejs-after-handling-load-data-event%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Since your result is an object which is containing one item, that item is an array called routes in this case you should loop through result.routes like so :



 <option v-for="routes in result.routes" v-bind:key="routes.name" v-bind:value="routes.name"> routes.name </option>


Extra Example :






new Vue(
el: '#app',
data: function()
return
selectedUser: '',
users: ,


,
mounted: function()
// this.loadData();
// this.timer = setInterval(this.loadData, 4000);
,

methods:
loadData: function()
axios.get('https://jsonplaceholder.typicode.com/users').then(response =>
this.users = response.data;

).catch(e =>

)



)

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
<meta charset="utf-8">
<title></title>
<script src="https://unpkg.com/vue@2.5.17/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://unpkg.com/vue-axios@2.1.4/dist/vue-axios.min.js"></script>
</head>

<body>
<div id="app">
<select name="userSelect" v-model="selectedUser">
<option v-for="user in users" v-bind:key="user.id" v-bind:value="user.name"> user.name </option>
</select>
<button @click="loadData">Load data</button>
</div>
</body>

</html>








share|improve this answer


















  • 1





    I tried it now it works. It makes sense too, I didn't think that it was an object and not an array. Thank you so much!

    – M. H
    Nov 15 '18 at 20:29






  • 1





    you're welcome i added also a working example for futur askers

    – Boussadjra Brahim
    Nov 15 '18 at 20:30















2














Since your result is an object which is containing one item, that item is an array called routes in this case you should loop through result.routes like so :



 <option v-for="routes in result.routes" v-bind:key="routes.name" v-bind:value="routes.name"> routes.name </option>


Extra Example :






new Vue(
el: '#app',
data: function()
return
selectedUser: '',
users: ,


,
mounted: function()
// this.loadData();
// this.timer = setInterval(this.loadData, 4000);
,

methods:
loadData: function()
axios.get('https://jsonplaceholder.typicode.com/users').then(response =>
this.users = response.data;

).catch(e =>

)



)

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
<meta charset="utf-8">
<title></title>
<script src="https://unpkg.com/vue@2.5.17/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://unpkg.com/vue-axios@2.1.4/dist/vue-axios.min.js"></script>
</head>

<body>
<div id="app">
<select name="userSelect" v-model="selectedUser">
<option v-for="user in users" v-bind:key="user.id" v-bind:value="user.name"> user.name </option>
</select>
<button @click="loadData">Load data</button>
</div>
</body>

</html>








share|improve this answer


















  • 1





    I tried it now it works. It makes sense too, I didn't think that it was an object and not an array. Thank you so much!

    – M. H
    Nov 15 '18 at 20:29






  • 1





    you're welcome i added also a working example for futur askers

    – Boussadjra Brahim
    Nov 15 '18 at 20:30













2












2








2







Since your result is an object which is containing one item, that item is an array called routes in this case you should loop through result.routes like so :



 <option v-for="routes in result.routes" v-bind:key="routes.name" v-bind:value="routes.name"> routes.name </option>


Extra Example :






new Vue(
el: '#app',
data: function()
return
selectedUser: '',
users: ,


,
mounted: function()
// this.loadData();
// this.timer = setInterval(this.loadData, 4000);
,

methods:
loadData: function()
axios.get('https://jsonplaceholder.typicode.com/users').then(response =>
this.users = response.data;

).catch(e =>

)



)

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
<meta charset="utf-8">
<title></title>
<script src="https://unpkg.com/vue@2.5.17/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://unpkg.com/vue-axios@2.1.4/dist/vue-axios.min.js"></script>
</head>

<body>
<div id="app">
<select name="userSelect" v-model="selectedUser">
<option v-for="user in users" v-bind:key="user.id" v-bind:value="user.name"> user.name </option>
</select>
<button @click="loadData">Load data</button>
</div>
</body>

</html>








share|improve this answer













Since your result is an object which is containing one item, that item is an array called routes in this case you should loop through result.routes like so :



 <option v-for="routes in result.routes" v-bind:key="routes.name" v-bind:value="routes.name"> routes.name </option>


Extra Example :






new Vue(
el: '#app',
data: function()
return
selectedUser: '',
users: ,


,
mounted: function()
// this.loadData();
// this.timer = setInterval(this.loadData, 4000);
,

methods:
loadData: function()
axios.get('https://jsonplaceholder.typicode.com/users').then(response =>
this.users = response.data;

).catch(e =>

)



)

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
<meta charset="utf-8">
<title></title>
<script src="https://unpkg.com/vue@2.5.17/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://unpkg.com/vue-axios@2.1.4/dist/vue-axios.min.js"></script>
</head>

<body>
<div id="app">
<select name="userSelect" v-model="selectedUser">
<option v-for="user in users" v-bind:key="user.id" v-bind:value="user.name"> user.name </option>
</select>
<button @click="loadData">Load data</button>
</div>
</body>

</html>








new Vue(
el: '#app',
data: function()
return
selectedUser: '',
users: ,


,
mounted: function()
// this.loadData();
// this.timer = setInterval(this.loadData, 4000);
,

methods:
loadData: function()
axios.get('https://jsonplaceholder.typicode.com/users').then(response =>
this.users = response.data;

).catch(e =>

)



)

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
<meta charset="utf-8">
<title></title>
<script src="https://unpkg.com/vue@2.5.17/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://unpkg.com/vue-axios@2.1.4/dist/vue-axios.min.js"></script>
</head>

<body>
<div id="app">
<select name="userSelect" v-model="selectedUser">
<option v-for="user in users" v-bind:key="user.id" v-bind:value="user.name"> user.name </option>
</select>
<button @click="loadData">Load data</button>
</div>
</body>

</html>





new Vue(
el: '#app',
data: function()
return
selectedUser: '',
users: ,


,
mounted: function()
// this.loadData();
// this.timer = setInterval(this.loadData, 4000);
,

methods:
loadData: function()
axios.get('https://jsonplaceholder.typicode.com/users').then(response =>
this.users = response.data;

).catch(e =>

)



)

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
<meta charset="utf-8">
<title></title>
<script src="https://unpkg.com/vue@2.5.17/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://unpkg.com/vue-axios@2.1.4/dist/vue-axios.min.js"></script>
</head>

<body>
<div id="app">
<select name="userSelect" v-model="selectedUser">
<option v-for="user in users" v-bind:key="user.id" v-bind:value="user.name"> user.name </option>
</select>
<button @click="loadData">Load data</button>
</div>
</body>

</html>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 20:25









Boussadjra BrahimBoussadjra Brahim

8,2143733




8,2143733







  • 1





    I tried it now it works. It makes sense too, I didn't think that it was an object and not an array. Thank you so much!

    – M. H
    Nov 15 '18 at 20:29






  • 1





    you're welcome i added also a working example for futur askers

    – Boussadjra Brahim
    Nov 15 '18 at 20:30












  • 1





    I tried it now it works. It makes sense too, I didn't think that it was an object and not an array. Thank you so much!

    – M. H
    Nov 15 '18 at 20:29






  • 1





    you're welcome i added also a working example for futur askers

    – Boussadjra Brahim
    Nov 15 '18 at 20:30







1




1





I tried it now it works. It makes sense too, I didn't think that it was an object and not an array. Thank you so much!

– M. H
Nov 15 '18 at 20:29





I tried it now it works. It makes sense too, I didn't think that it was an object and not an array. Thank you so much!

– M. H
Nov 15 '18 at 20:29




1




1





you're welcome i added also a working example for futur askers

– Boussadjra Brahim
Nov 15 '18 at 20:30





you're welcome i added also a working example for futur askers

– Boussadjra Brahim
Nov 15 '18 at 20:30



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327188%2fpopulating-options-for-select-using-v-for-vuejs-after-handling-load-data-event%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto