doublesecretagency / craft-googlemaps

Google Maps plugin for Craft CMS - Maps in minutes. Powered by the Google Maps API.
https://plugins.doublesecretagency.com/google-maps/
Other
10 stars 8 forks source link

Cannot assign null to property $results, error with 4.2.0 for CraftCMS 4 #54

Closed aullah closed 2 years ago

aullah commented 2 years ago

Hi there,

There seems to be an issue with version 4.2.0 of the Google Maps plugin. When searching for certain postcodes (in the UK), the following error is returned:

Exception 'TypeError' with message 'Cannot assign null to property doublesecretagency\googlemaps\events\GeocodingEvent::$results of type array'

I believe this response is given to any response where a location cannot be found. The line of code I'm using (which can be used to replicate the issue) is GoogleMaps::lookup('NG1 6FD')->one().

I'm trying to catch the exception but it doesn't seem to catch either (perhaps something I need to look into further).

Thank you and kind regards, Arif.

aullah commented 2 years ago

I've been able to successfully catch the exception by checking for both an Exception and Throwable, like so:

try {
    $location = GoogleMaps::lookup('NG1 6FD')->one();
} catch (\Exception|\Throwable $e) {
    $location = false;
}
lindseydiloreto commented 2 years ago

Good catch, this was a typecasting error from the Craft 4 update. Thanks for reporting it!

It's now been fixed, update to v4.2.1 whenever you get a chance. 👍