Error "Property 'coords' does not exist on type Geoposition" updating the Geolocation plugin

Hi,
after updating the Geolocation plugin in a Ionic4/Cordova app, I have this error:

[ng]   Property 'coords' does not exist on type 'PositionError'.
[ng] ...: error TS2339: Property 'coords' does not exist on type 'Geoposition | PositionError'.

I have always used the plugin in this way:

this.geolocation.watchPosition(geo_options)
      .subscribe(position => {
          this.currentLatitude = position.coords.latitude;
          this.currentLongitude = position.coords.longitude;

The Cordova plugin and the Ionic module are:

cordova-plugin-geolocation 4.0.2 "Geolocation"
"@ionic-native/geolocation": "^5.28.0",

Why the Geoposition object doesn’t have the property coords?

Claudio

I think there is a problem with the version 5.28.0 of the Ionic native module, because with this one the app works well:

npm install @ionic-native/geolocation@5.27.0

I would phrase it differently. I think 5.28.0 acknowledges something that both your current code and previous versions failed to, which is that you won’t always be getting a Geoposition there. You might have a PositionError instead, which indeed has no coords.

I would do something like:

if ("coords" in position) {
  // proceed as before
} else {
  // ruh roh we have a PositionError
}
2 Likes