Cannot read property 'nativeOption' of undefined


Topic: Cannot read property 'nativeOption' of undefined

Falk asked 4 years ago

Expected behavior

When calling this code:

const singleSelect = document.querySelector('#singleSelection');

const singleSelectInstance = mdb.Select.getInstance(singleSelect);

singleSelectInstance.setValue('4');

Expecting the selected <option> to be updated to the one which has the given value as the value of the "value" attribute.

Actual behavior

The select is not updated due to the following error:

TypeError: Cannot read property 'nativeOption' of undefined

In our case, the select instance in question is rendered at runtime from a template after page load, which in general has been causing us some issues with having to invoke instances of MDB elements manually after the rendering. Not sure if this may be related.

Resources (screenshots, code snippets etc.)

The error points to this line in the MDB source:

{key:"_selectByValue",value:function(t){this.options.find(function(e){return e.value===t}).nativeOption.selected=!0}}

Upon debugging the select instance we do see that there's an options property, and the nativeOption property appears to exist for each option.


Grzegorz Bujański staff answered 4 years ago

From what I can see it works fine. Check out this snippet: https://mdbootstrap.com/snippets/standard/grzegorz-bujanski/2884417#js-tab-view

If the select instance is rendered at runtime from a template after page load using DOMContentLoaded event should help:

window.addEventListener('DOMContentLoaded', () => {
    const singleSelect = document.querySelector('#singleSelection');
    const singleSelectInstance = mdb.Select.getInstance(singleSelect);
    singleSelectInstance.setValue('4');
});

Falk answered 4 years ago

This occurs when you attempt to set the selected option, but the provided value doesn't exist for any of the options in the select.

It can be reproduced by simply providing a value which doesn't exist among the options.

https://mdbootstrap.com/snippets/standard/falk/2882248

The console shows the same error. An ideal solution here would probably be to provide a more coherent error message, which orientates a developer towards the actual problem.


Grzegorz Bujański staff answered 4 years ago

We do not plan to introduce such messages at the moment. This would require a lot of code to be added. Unfortunately, this will have an impact on the performance of applications in which MDB will be used.


Falk answered 4 years ago

try {
    // existing code
} catch(e) {
    // check for known case
    if (e instanceof TypeError && e.message.indexOf('nativeOption') !== -1) {
        // chose RangeError here, but you may have internal MDB error classes
        throw new RangeError('Option with given value not found in select element');
        // or don't throw and just ignore call to select an unknown option
    }

    throw e; // throw original exception if anything else
}

So, not that difficult. Not sure about the overhead you're referring to, this code only executes when the error occurs.


Grzegorz Bujański staff commented 4 years ago

Remember that each additional line of code increases the size of the mdb.min.js file. And the larger the mdb.min.js file, the longer the page will load. Especially in the case of mobile internet in a place with poor coverage. That's a lot of code for just one case. And there will be thousands of such cases. We are not currently planning to implement such a solution.


Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue
  • User: Free
  • Premium support: No
  • Technology: MDB Standard
  • MDB Version: 3.2.0
  • Device: MacBook Pro
  • Browser: Chrome 88.0.4324.150
  • OS: MacOS 11.2.1
  • Provided sample code: No
  • Provided link: No
Tags