trevoreyre / autocomplete

Accessible autocomplete component for vanilla JavaScript and Vue.
https://autocomplete.trevoreyre.com
MIT License
430 stars 75 forks source link

TypeError: results.forEach is not a function #20

Closed pmk65 closed 5 years ago

pmk65 commented 5 years ago

I'm getting a "TypeError: results.forEach is not a function" in autocomplete.cjs.js:445:13 when I try setting up a asynchronous API call to https://restcountries.eu/rest/v2/name/ The error occurs when no match is found, and I think it's because restcountries.eu returns a different JSON structure when there's no valid match.

Example call where matches are found. https://restcountries.eu/rest/v2/name/den

Example call where NO MATCH is found. https://restcountries.eu/rest/v2/name/qzx

trevoreyre commented 5 years ago

Yeah, the search function expects you to return an array. So I think in the case where the API returns a no match result, you’ll need to intercept the result and return an empty array from your promise.

Try that out and let me know if it works for you.

pmk65 commented 5 years ago

@trevoreyre I had to test for the existence of one of the keys in the "not found" return object. But then it worked. I don't know if this is a special case (Usually API's return the same or a sub structure, no matter if there's a match or not) since restcountries.eu returns two different result structures.

Question: Is there any methods to destroy/remove the autocomplete instance?

BTW: Thanks for a great script. I really like the simplicity of the setup/options.

trevoreyre commented 5 years ago

@pmk65 Glad to hear you go it working. I don't think this is a particularly rare case. But I don't know how I would make the component handle that generally, since each API is going to have a different shape for successful vs unsuccessful responses.

As for your question, no there isn't currently any method for destroying and cleaning up the autocomplete instance. I assume you're using the autocomplete-js package? If you want to create a separate issue for the cleanup method, I'd be happy to look into it. I'm going to go ahead and close this issue since you got everything working.