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 3 years ago.
I am getting data from an API. I need this response data to use outside of axios, not inside any function but globally available in JavaScript, HTML DOM.
let responseData = [];
axios
.get(url)
.then(res => {
responseData.push(res.data.data);
});
console.log(responseData);
console.log(responseData); gives the output:
[]
inside this array is another array of objects [{},{},{}]
[[{},{},{}]]
But the format I need is [{},{},{}]
Any idea how should I approach this?
try in this way:
responseData=responseData.concat(res.data.data);
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)
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 months ago.
I have problem with this function, it returns a console log, but does not push the data to the array, how can I fix this, I know the problem is related to promises but I don't know how I can fix it.
// Parse XLSX DATA function
function parseXLSXData(myFile) {
// Using the Read XLSX file cdn JS
let parseData = [];
readXlsxFile(myFile).then((rows) => {
// `rows` is an array of rows
// each row being an array of cells.
console.log(rows);
parseData.push(rows);
});
return parseData;
}
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.
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.
I am just trying to access data inside then() method.
var dataset;
fetch ('https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/GDPdata.json').then (response=>response.json()).then(val=>{
dataset=JSON.stringify(val.data)
});
When i console out it outside then() method it give me this:
undefined;
But when I try to console it out inside , it give me my desired result;
I just want to store my desired result in my dataset variable. How?
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();
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 8 years ago.
I'm pretty confused by the JavaScript scoping. I have a call to FB.api (shown below) that I would like to return a response object:
(function() {
var permissions;
FB.api("/me/permissions", "get", {access_token: options.token}, function(r) {
permissions = r;
});
console.log(permissions);
})();
Ideally, I'd like to assign FB.api to a variable, and use the anonymous function to return the r component, but it appears like that's not doable.
Why is permissions undefined? Is there a way to set permissions in the parent scope?