A few things:
- For your issue, BE is being geocoded as "Bé, Togo" which is a neighborhood in "Lome, Togo". But since you set the entity type to country, its returnign the boundary for Togo. Bing Maps geocoder often sets the most precise entity type item as the first result. For example, if it has a city and country result with equal accuracy score, the city will almost always be ordered first since it is more precise. This is done as Bing Maps is designed with the needs of a search engine in mind. Using Bing Maps there really isn't a good way to better handle this than to have the user select the result they want. Perhaps having an autosuggest experience would be good.
- A separate issue in your code that will occur for many users, currently you are a 100ms timeout to wait for the geocode request to complete. The geocoding service will not return a response that fast 100% of the time for all users. Put the code for calling the GeoData API into the callback of the geocode search to ensure the GeoData API is called after the geocode call.
Another option if you are open to using other platforms is to try Azure Maps. Boundary data is retrieved in a bit of a different way. With Azure Maps you have to geocode your query first and if the result has a boundary, an unique boundary ID will be provided which can be used to retrieve that boundary using the boundary service. This means that the boundary is always of the same entity type as the query. Additionally, the geocoder in Azure Maps works a bit different from Bing Maps and orders results differently and works a bit better with well structured queries. Here is a sample app that has a search bar you can use to try this out and see if it better meets your needs: https://azuremapscodesamples.azurewebsites.net/?sample=Search%20for%20boundaries