[SOMETIMES WORK, SOMETIMES DON'T] Issue with variables in Geolocation.watchposition - IONIC3

Hi,

I’m having an issue with the variables in my app.component.ts appearing as ERROR [object object], while when I just do a console.log - everything seems fine.
The variable userLocation is correctly created in my provider…

I need help, is this an issue with watchPosition() or am I doing something wrong?

Here’s the code:

    let watch = this.geolocation.watchPosition();
    watch.subscribe((data) => {
     // data can be a set of coordinates, or an error (if an error occurred).
    this.locationProvider.userLocation.lat = data.coords.latitude;
    this.locationProvider.userLocation.lng = data.coords.longitude;
    console.log(data.coords.latitude, ' ', data.coords.longitude);
    });

This is the error I’m getting in console when trying to build it:
VM1745 ion-dev.js:156 ERROR TypeError: Cannot set property 'lat' of undefined at SafeSubscriber._next (VM49 main.js:686) at SafeSubscriber.__tryOrUnsub (VM48 vendor.js:35724) at SafeSubscriber.next (VM48 vendor.js:35671) at Subscriber._next (VM48 vendor.js:35611) at Subscriber.next (VM48 vendor.js:35575) at t.invoke (VM47 polyfills.js:3) at Object.onInvoke (VM48 vendor.js:5134) at t.invoke (VM47 polyfills.js:3) at r.runGuarded (VM47 polyfills.js:3) at VM47 polyfills.js:3

[Cannot read property ‘latitude’ undefined ]

my ts code

import { Geolocation } from '@ionic-native/geolocation';

 private lat: any;
 private lng: any;
 constructor( private geolocation: Geolocation ) {

 let watch = this.geolocation.watchPosition();
watch.subscribe((data) => {
  
 // data can be a set of coordinates, or an error (if an error occurred).
 this.lat= data.coords.latitude;
 this.lng= data.coords.longitude;
},(error)=>{console.log(error);});

  }

please help me

HI @salomon022, where did you put this?

I solved it, but it sometimes work and sometimes don’t.
I think my issue is with sync and async functions happening, but I’m not sure.

@salomon022, what version of the plugin are you using?
It might be an issue there as well. I’m developing on three different computers (2 PCs and a Mac), so it might be an issue with the plugins and their compat.