global variable in javascript not working; loses data [duplicate] - javascript

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
var feed;
$.getJSON('short.json', function(data) {
feed = data.items;
console.log(feed);
});
console.log(feed);
I have this short code written above. I am expecting feed to be a global variable but once it comes out the function, it's undefined again. It prints out an object when inside. What am I doing wrong?
Thanks for the help.

The reason is that the getJSON() call is asynchronous. It won't run until AFTER the second console.log();

Related

Object has contents, as long as I don't want to specifically adress them [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)
How do I return the response from an asynchronous call?
(41 answers)
Console.log showing only the updated version of the object printed
(3 answers)
Closed 2 years ago.
I have this React project, in which I fetch data from a firebase database and construct a object called "statehelp" from the results. Now for some reason, if I console log the whole statehelp Object I get all its contents, however if I want to access statehelp.contents or statehelp.products they are undefined.
componentDidMount() {
var statehelp = {
content: {},
products: {}
}
getFarm(this.id).then(result => statehelp.content = result)
getProduct(this.id).then(result => statehelp.products = result)
console.log(statehelp)
console.log(statehelp.content)
}
That happens because console.log() is synchronous operation. It won't wait for your api call to happen. So don't use console.log() as a metric for identifying whether your object got populated or not.

Cant access variable outside of function even though its global [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 4 years ago.
I am attempting to access color options from a chrome extension, when I run this code, the second output is undefined, even though the variable is supposed to be able to be accessed by every part of the code because I defined it in the beginning.
var color;
function updateColor(){
chrome.storage.sync.get("favoriteColor", function(result) {
color = result.favoriteColor;
console.log(color);
});
console.log(color);
};
updateColor();
I have searched all over the internet and I am unable to come up with a solution, any help would be greatly appreciated :)

how to call a variable defined below it in Javascript [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
i have the following code and i'm trying to console log a variable called trt i created below it: how can i get trt in console log??
thanks
this.rows = this.dataService.SetItem('added-items',());{
console.log(trt);
this.dataService.GetJson().subscribe((result)=>
{
this.rows=(JSON.parse(result['_body']));
var trt=(result['_body']);
//this.rows = this.dataService.SetItem("data",this.rows);
})
thanks!!
You need to log it to the console after you define it.
this.rows = this.dataService.SetItem('added-items',());{
//console.log(trt);
this.dataService.GetJson().subscribe((result)=>
{
this.rows=(JSON.parse(result['_body']));
var trt=(result['_body']);
console.log(trt);
//this.rows = this.dataService.SetItem("data",this.rows);
})
Edit: you cannot call a variable that has not been defined yet and expect it to not be undefined.

javascript variable value lost when outside object [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 7 years ago.
This may sound a newbie question but I'm having really hard time with variable scopes in Javascript.
I have the following JS snippet:
<script>
window.IDFVPlugin.getIdentifier(function(result){ uuid = result; });
alert(uuid);
</script>
I want to use the variable uuid anywhere in the script outside the window object. uuid returns the correct value only when inside the object and the value is lost when outside. So the alert above will log an undefined variable error.
You use a callback function. Result should be used inside of callback body. If you try to use it immediately after main function call - it will not be yet available
window.IDFVPlugin.getIdentifier(function(result){
uuid = result;
alert(uuid);
});

Getting a Javascript variable out of a $.get statement [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
How to return AJAX response Text? [duplicate]
(2 answers)
Closed 9 years ago.
EDIT: Please feel free to delete, I have found an appropriate answer in the duplicates mentioned above. Apologies.
I have the following code, and can't seem to dig the variables out correcly:
$('#button').click(function() {
alert(getRemaining(0));
}
function getRemaining(i){
var x;
$.get('files/remaining.txt', function(file){
x = file.split(",");
});
return x[i]
}
My alert just keeps coming out as undefined. What am I doing wrong?
the .get that you run is an asynchronous function. This means that execution of your code will continue on past it BEFORE it completes. The callback function that you pass into .get will be called once it is finished (this is the main reason for providing a callback).
This code will alert once the .get has returned.
$.get('files/remaining.txt', function(file){
x = file.split(",");
alert(x[0]);
});

Categories

Resources