This is likely a beginners question, but I'm having trouble using 'this' for global variables. I am using Ionic 4. And I'm trying to post a location to my database.
I have declared the following global variable in my class:
export class HomePage {
myVar:any;
I can use it in a function like this:
mainFunction(coordinates) {
this.myVar = coordinates.coords.latitude;
The problem is that when i go one function deeper. The variable can not be accessed with 'this.'.
mainFunction(coordinates) {
navigator.geolocation.getCurrentPosition(this.helperFunction);
console.log(this.myVar);
helperFunction(coordinates) {
this.myVar = coordinates.coords.latitude;
For some reason a nested function can not read or write the global variable. I get the following error:
ERROR TypeError: Cannot read property 'myVar' of null
How do I pass the value of myVar back to mainFunction? Thanks in advance.
I am calling the mainFunction with a button in a .html
<ion-button expand="block" (click)="mainFunction()"></ion-button>