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.

Issue with compareOption Uncaught TypeError: Cannot read property 'toLowerCase' of undefined

See original GitHub issue

I found an issue with compareOption function when changes getOptionValue and getOptionLabel to a different (value|label) structure.= using with AsyncCreatable.

Follow the example to replicate.

The options are loaded from an endpoint using callback handler, with the JSON structure: [{ id: 1, name: 'teste1' }, { id: 2, name: 'teste2' }, { id: 3, name: 'teste3' } ]

const customFilter = createFilter({
      ignoreCase: false,
      ignoreAccents: false,
      trim: false,
      matchFrom: 'any',
    });

<AsyncCreatableSelect
        name="tag"
        id="tag"
        isMulti
        isClearable
        placeholder="Marque algumas tags para este registro"
        getOptionValue={option => option.id}
        getOptionLabel={option => option.id}
        filterOption={customFilter}
        loadingMessage={e => `Carregando opções próximas de '${e.inputValue}'`}
        noOptionsMessage={e => `Sem opções disponíveis para escolha`}
        formatCreateLabel={e => `Criar uma nova tag com rótulo '${e}'`}
        components={{ Option: Option, ...makeAnimated() }}
        loadOptions={loadOptions}
       
      />

Uncaught TypeError: Cannot read property ‘toLowerCase’ of undefined at compareOption (Creatable.js?067d:40) at eval (Creatable.js?067d:51) at Array.some (<anonymous>) at isValidNewOption (Creatable.js?067d:50) at Creatable.componentWillReceiveProps (Creatable.js?067d:133) at callComponentWillReceiveProps (react-dom.development.js?cada:11528) at updateClassInstance (react-dom.development.js?cada:11720) at updateClassComponent (react-dom.development.js?cada:13154) at beginWork (react-dom.development.js?cada:13825) at performUnitOfWork (react-dom.development.js?cada:15864)

When I got the function:

var compareOption = function compareOption(inputValue, option) { var candidate = inputValue.toLowerCase(); return option.value.toLowerCase() === candidate || option.label.toLowerCase() === candidate; };

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:25
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
nehaleemcommented, Oct 10, 2018

Same issue here. Cannot use Creatable because it doesnt respect getOptionValue and getOptionLabel props.

Uncaught TypeError: Cannot read property 'toLowerCase' of undefined
at compareOption (Creatable.js?067d:40)
3reactions
jkeruzeccommented, Sep 3, 2018

I think, it has already multiple PR about that subject …

I also did it by overriding :

 getNewOptionData={(inputValue, optionLabel) => {
                return {
                    id: inputValue,
                    value: optionLabel,
                    __isNew__: true};
            }}
            isValidNewOption={(inputValue, selectValue, selectOptions) => {
                
                const compareOption = (inputValue, option) => {
                    
                    const getOptionValue = (option) => option.id;
                    const getOptionLabel = (option) => option.value;
                    
                    const candidate = inputValue.toLowerCase();
                    return (
                        getOptionValue(option).toLowerCase() === candidate ||
                        getOptionLabel(option).toLowerCase() === candidate
                    );
                }
                
                return !(!inputValue ||
                selectValue.some(option => compareOption(inputValue, option)) ||
                selectOptions.some(option => compareOption(inputValue, option)));
                
            }}
            onCreateOption={inputValue => this.createNewElementFromSelect(inputValue)}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncaught TypeError: Cannot read property 'toLowerCase' of ...
toLowerCase() is undefined? What debugging have you tried already? It seems either paletteName isn't a property on the elements in the palettes ...
Read more >
TypeError: Cannot read property 'toLowerCase' of null
This will throw the ReferenceError you are seeing when we try to apply a string method to the return value. null has no...
Read more >
Uncaught TypeError: Cannot read property ... - WordPress.org
Hello, I add a form with country field but I'm facing the error. Uncaught TypeError: Cannot read property 'toLowerCase' of undefined.
Read more >
Issue with compareOption Uncaught TypeError: Cannot read ...
Issue with compareOption Uncaught TypeError : Cannot read property 'toLowerCase' of undefined. react-select. 01 September 2018 Posted by vrbarros.
Read more >
Cannot read property 'toLowerCase' of Undefined in JS
The "Cannot read property 'toLowerCase' of undefined" error occurs when calling the ... const str = undefined; // ⛔️ TypeError: Cannot read properties...
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