question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

v-model on <select> sets the value to undefined if the <select> is lacking the matching <option>

See original GitHub issue

Let’s say we have a variable called someVar in our component - it is properly defined and its initial value is 1:

...
data: {
    someVar: 1
}
...

In our template there is a select tag with 3 options bound to someVar with v-model:

<select v-model="someVar">
    <option value="1">Option 1</option>
    <option value="3">Option 3</option>
    <option value="5">Option 5</option>
</select>

At this point everything works as expected. However, if we then change the value of someVar to a value that is not covered by any of the option tags (2 in this example), v-model will set someVar to undefined instead:

...
created: function() {
  setTimeout(function(){
    this.someVar = 2;
    // this.someVar becomes undefined instead
  }.bind(this), 2000);
}
...

Here is a jsfiddle showing the issue: https://jsfiddle.net/0vpqny9y/4/

What I expected to happen was that the value is set properly and the select tag becomes empty.

While the setTimeout example may be silly, where it’s becoming a pain to me is when I load both the options and the model that has a property bound to the select at the same time, which results in a race condition:

  1. Options load first, model loads second, the option is present so everything works fine.
  2. Model loads first, the bound value is set to whatever was loaded through AJAX, but since there are no options yet the value is instead set to undefined, then the options load by this time the original value is lost

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:12
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
paulredmondcommented, May 18, 2017

Not sure if this is the “right” way, but I got around this issue with async options by using v-if on the component to not render it until the async operation was complete.

https://jsfiddle.net/paulredmond/a085bpmn/2/

8reactions
jonjanischcommented, Jan 29, 2017

I was also confused by this. If this is the expected behavior, it may be helpful to mention this in the Form Input Bindings -> Select section of the documentation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting value in Vue.js select gives `undefined` value
Solution is simple: https://github.com/vuejs/vue/issues/4524. v-model on <select> sets the value to undefined if the <select> is lacking the ...
Read more >
Getting and Setting | Vue Select
The most common use case for vue-select is to have the chosen value synced with a parent component. vue-select takes advantage of the...
Read more >
Table | Components - BootstrapVue
The key for selecting data from the record in the items array. Required when setting the fields via an array of objects. The...
Read more >
Adding a new todo form: Vue events, methods, and models
Add a data() method to our ToDoForm component object that returns a label field. We can set the initial value of the label...
Read more >
Field - VeeValidate
The Field component renders an HTML input tag if not specified otherwise. ... You may use v-model here or bind the selected attributes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found