I'm facing an issue with a button component that should trigger a function on click event:
<button pButton type="button" label="Add EchoBeacon" (click)="insertPoint()">
constructor(private mappaService: MappaService) {}
...
insertPoint() {
this.map.removeInteraction(this.interaction);
this.select.getFeatures().clear();
this.map.removeInteraction(this.select);
this.interaction = new ol.interaction.Draw({
type: /** @type {ol.geom.GeometryType} */ 'Point',
source: this.echoBeacons.getSource()
});
this.map.addInteraction(this.interaction);
this.interaction.on('drawend', function(e) {
var feature = e.feature;
var geometry = feature.getGeometry();
this.coordinates = geometry.getCoordinates();
this.mappaService.savePoint(this.coordinates[0], this.coordinates[1])
.subscribe(
data => console.log('Added'),
error => console.error('Error: ' + error),
() => console.log('Completed!')
);
}
Here's the method service being used:
savePoint(x: number, y :number): Observable<any[]>{
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
let serverPostDbUrl ="http://172.16.32.1:3000/beacon";
let body = `{"x": ${x}, "y": ${y}}`;
return this.http.post(`${serverPostDbUrl}`, body, options)
.map((res:Response) => res.json())
.catch((error:any) => Observable.throw(error.json().error || "Server error"));
}
However, upon clicking the button, I receive an error in the console stating 'cannot read a property of undefined', indicating that mappaService is not defined within the insertPoint() function.