Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutocompleteInput throws an error if resource[optionText] isn't a string #2991

Closed
developerium opened this issue Mar 13, 2019 · 8 comments
Closed
Labels

Comments

@developerium
Copy link
Contributor

developerium commented Mar 13, 2019

Hi guys

I noticed in here, if the result field from api response is null, (usually when first input is put in page), then suggestionLabel.toString() will throw an error saying null doesn't have method toString.

What do you think about this? Should we put a default filter to avoid being null, or convert to string differently?

@djhi
Copy link
Collaborator

djhi commented Mar 13, 2019

if the result field from api response is null, (usually when first input is put in page)

I don't understand this.

Should we put a default filter to avoid being null

Why are you referring the filter ?

@developerium
Copy link
Contributor Author

developerium commented Mar 14, 2019

This is the code I'm using to get around the error

<ReferenceInput
    {...props}
    source={source}
    reference="User"
    filterToQuery={searchText => ({ name: searchText, is_employee: 1 })}
  >
    <AutocompleteInput optionText={item => (item.name ? item.name : '')} />
  </ReferenceInput>

In the autocomplete, for the optionText prop, if I just gave optionText="name" , and api gave a response, in which even one row has name=null , this line will throw an error.

Because it is trying to run .toString on null .

@Kmaschta
Copy link
Contributor

Kmaschta commented Mar 14, 2019

We surely can improve this error message, but we'll not be able to do much.
If the data we need to display the item isn't available, we can at best remove it from the result list.

@Kmaschta Kmaschta changed the title Autocomplete suggestion label when field is null AutocompleteInput throws an error if resource[optionText] isn't a string Mar 14, 2019
@developerium
Copy link
Contributor Author

Yes I agree, some missing data should not crash the app.
What is the best way to handle this? Maybe I can contribute if you put me in the right direction

@fzaninotto
Copy link
Member

I think the fix is as easy as adding an empty string ('') as third parameter to the get() call line 183 of AutocompleteInput:

getSuggestionText = suggestion => {
if (!suggestion) return '';
const { optionText, translate, translateChoice } = this.props;
const suggestionLabel =
typeof optionText === 'function'
? optionText(suggestion)
: get(suggestion, optionText);
// We explicitly call toString here because AutoSuggest expect a string
return translateChoice
? translate(suggestionLabel, { _: suggestionLabel }).toString()
: suggestionLabel.toString();
};

Lodash.get() uses the third parameter as a default value.

@Kmaschta
Copy link
Contributor

IMO it's a better idea to filter the erroneous results and display an error in the console.

@developerium It's up to you! PR are welcome :)

@developerium
Copy link
Contributor Author

@fzaninotto I agree, will you put this in your next release or should I make a pull request? since it's a minor change?

@fzaninotto
Copy link
Member

you can make a pull request against master for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants