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)
Closed 2 years ago.
var jsonObj= [{}]
function read_directory(){
foreach files in directory{
//do stuff to file
jsonObj.push(newdata) //newdata is also an json object
console.log(jsonObj) //this returns the current object after each push
}
console.log(jsonObj) //this returns an empty json object
}
Why does the object appear to be empty when it is called outside of the loop.
When it is called inside of the loop, the data is pushed correctly. But when I want to save the complete object after the loop it is empty.
Related
This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Using async/await with a forEach loop
(33 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 11 months ago.
I have this code in my Node.js application.
The log inside forEach gives desired result but the one outside forEach gives an empty array.
let biddedListings = [];
bids.forEach(async (bid) => {
const listing = await ListingModel.find({ _id: bid.bidOn });
biddedListings.push(listing[0]);
// console.log("bidded", biddedListings);
});
console.log("bidded", biddedListings);
Whys is it that the same global variable is giving different results? What am I doing wrong here?
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
I am trying to print the username from the given search result array but it does not print anything. Currently, I am trying to append the value from all_users array of loop with key and value format such as username: value.user.username, and now i am printing with another loop, but it's now working.
Please check the below attachment for array coming
$(search_result).each(function(index, result) { // Not working this console.log(result.username); });
The above line must be inside your ajax call's success function definition.
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 5 years ago.
I have a function that I call which hits an API and returns a JSON, which I am calling getData. Inside that function is a nested function that processes the JSON and saves it to a new variable processedData when it's done making the request from the API.
I'd then like to return that processed JSON, but the console is telling me it's undefined. I didn't declare the variable so it should be treated as global. Why am I still having this issue?
The code won't work with the snippet because the API is only local, but processedData essentially looks like this: {'A': '123'}
function hitAPI() {
var getData = $.get('http://127.0.0.1:5000/myroute');
getData.done(function() {
processedData = (JSON.parse(getData['responseJSON']['data']));
});
return processedData;
};
var x = hitAPI()
console.log(x);
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)
Closed 6 years ago.
I have a promise/global object that outputs as follows in the console:
console.log(globalInfo);
Promise
__proto__:Promise
[[PromiseStatus]]:"resolved"
[[PromiseValue]]:globalInfo
item1:"12345"
item2:"something else"
item3:"www.stackoverflow.com"
__proto__:Object
And am successfully pulling out the value for "item2" as such:
globalInfo.then(function(val) {
newVar = val.item2;
console.log('newVar inside: ' + newVar);
});
console.log('newVar outside: ' + newVar);
But, I am not able to pass that variable/value outside of the then() function. Nothing outputs to the console for the second console.log ("newVar outside").
How do I make the variable "newVar" with the the value for "item2" available globally outside of the then() function?
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 8 years ago.
I want to store a value from a JSON object in a global variable to use later in my code. Apparently this does not work even though it should make the country variable global.
$.getJSON(reverseGeoRequestURL, function(reverseGeoResult){
window.country = reverseGeoResult.results[0].locations[0].adminArea1;
});
console.log(window.country);
The getJSON call is asynchronous. Put that console.log call inside the callback and it will not be undefined.