My service function is not being recognized by my component
import { Injectable } from '@angular/core';
import { ToastController } from '@ionic/angular';
@Injectable({
providedIn: 'root'
})
export class LocationService {
private coordinates: any;
constructor(
private toastCtrl: ToastController
) { }
set setCoords(coords: any) {
this.coordinates = coords;
this.toastCtrl.create({
message: `Coordinates Assigned ${this.coordinates.latitude},${this.coordinates.longitude}`
}).then(alertCtrl => {
alertCtrl.present();
});
}
get getCoords(): any {
return this.coordinates;
}
}
This is the component (app.component.ts
) where I am attempting to utilize the function:
Geolocation.getCurrentPosition({ enableHighAccuracy: true, timeout: 5000 }).then(loc => {
alert('Latitude: ' + loc.coords.latitude + 'Longitude: ' + loc.coords.longitude);
console.log('Location: ', loc);
this.locationService.setCoords(loc);
}).catch(err => {
alert(err);
console.log('Error while getting location:', err);
});
Despite everything seemingly in order, I encounter the following error which puzzles me as to why the function is not being detected. Even after restarting the app and trying again, the issue persists.
Error while getting location: TypeError: this.locationService.setCoords is not a function
at app.component.ts:56
at ZoneDelegate.invoke (zone-evergreen.js:359)
at Object.onInvoke (core.js:34201)
at ZoneDelegate.invoke (zone-evergreen.js:358)
at Zone.run (zone-evergreen.js:124)
at zone-evergreen.js:855
at ZoneDelegate.invokeTask (zone-evergreen.js:391)
at Object.onInvokeTask (core.js:34182)
at ZoneDelegate.invokeTask (zone-evergreen.js:390)
at Zone.runTask (zone-evergreen.js:168)