Closed pmk65 closed 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.
@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.
@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.
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 becauserestcountries.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