Can't acces global variable with 'this' with nested function [duplicate] - javascript

This question already has answers here:
How to access the correct `this` inside a callback
(13 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
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>

Related

How to handle returning an inner callback variable? [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
How do I access a variable inside a callback without reassigning it to a variable first?
For example, the following code works:
let volume = 0;
loudness.getVolume((err, vol) => {
volume = vol;
});
But what if I wanted it to be assignable directly to a const. The following returns undefined:
const volume = loudness.getVolume((err, vol) => vol));
The short answer is you can not. The callback function exists in it's own scope isolated from the rest of the code. The only way to extract that info to be used in the rest of the code is to assign it to a variable that exists in a parent scope.
In simple terms do what you did in your first example.

I can't read a member variable from within another member function [duplicate]

This question already has answers here:
How to access the correct `this` inside a callback
(13 answers)
Closed 6 years ago.
I have a trouble about scope, I knew the solution but this isn't the problem.
I want to know why the member variable xhrObj unreadable from within an another member function, although that variable is global variable for that member function ?
It is happening because xhrObj function onreadystatechage is asynchrous in nature and when it return after complete call this context is different inside the onreadystatechage() and hence this.xhrObj is not different.

Cannot assign value to variable by reference in Javascript [duplicate]

This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 6 years ago.
I would like to assign value to the variable IMLoginReq inside a ProtoBuf load function, but its not working, can anyone help?
var IMLoginReq;
protobuf.load("./pb/IM.Login.proto", (err, root) => {
// Obtain a message type
IMLoginReq = root.lookup("IM.Login.IMLoginReq");
console.log(IMLoginReq);//<== is not undefined
});
console.log(IMLoginReq);//<== is undefined
The load() method is asynchronous. As such the console.log at the end will happen before the load finishes. Instead of trying to treat this as procedural logic, which it is not, you should instead use the IMLoginReq inside the success method that you have.
Thats because you are trying to call it before its loaded. You should have a callback function like success so it will be there.
You can also use promises by omitting the callback:
protobuf.load("awesome.proto")
.then(function(root) {
...
});

Node/Javascript - variable access within multiple callbacks and for loop [duplicate]

This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 7 years ago.
I have a callback function and within that, a for loop containing another calllback.
I am struggling with understanding the variable scope. I need to access the out variable throughout the nested structure:
var out = {"foo":123};
persistence.getAllApiKeys(function(err, allKeys){
for (var prop in allKeys) {
out = {"baz":456};
persistence.getApiKeyValue("test", function(err2, value) {
out = {"success":true}; // <--does not update
});
}
console.log(out);
})
Outputs:
{ baz: 456 }
How can I access the out variable inside the final callback? i.e. set it to "success":true?
Classic aysnc.
console.log is executed before the callback for getApiKeyValue. Put the console inside the callback and you will see what's wrong.

Variable Scope inside a promise jquery [duplicate]

This question already has answers here:
How do JavaScript closures work?
(86 answers)
Closed 8 years ago.
Imagine you are doing the following inside a javascript function called Fetch.
function Fetch(context)
{
var request = $.ajax({...});
request.done(function(response)
{
// it looks like context is visible here and in Scope.
//
});
}
Can you explain why context is visible inside the callback function.?
context is local to Fetch. request is declared inside of Fetch, therefore context is available inside request

Categories