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?
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 8 months ago.
I am using fs.createReadStream to read CSV data and it is exhibiting some weird behaviour. I'm very confused as to why this is happening.
I have the following code:
const result = []
fs.createReadStream("path")
.pipe(csv()) //csv parser
.on("data", data => result.push(data))
.on("close", () => console.log(results)) // Showing results correctly here, 5000 rows
console.log(results) // Showing an empty array here []
I don't understand why the results array is going back to empty after the data was added correctly on close.
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 1 year ago.
I am writing some code for a program, but I'm not able to figure out how to assign the output of a fetch to a variable. Here's my code:
const fetch = require('node-fetch');
var totalNumOfPages;
fetch('https://api.hypixel.net/skyblock/auctions')
.then(res => res.json())
.then(totalPages => totalNumOfPages = totalPages)
console.log(totalNumOfPages)
The output is undefined, rather than the value of "totalPages" taken from the api. How can I assign the value of totalPages to the var totalNumOfPages.
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.
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
Can somebody explain me how to solve this problem?
let konyvSzam=0;
db.checkPeldanySzam(rentISBN,(konyv) => {
konyvSzam=konyv.Peldanyszam;
});
console.log("Nr of books");
console.log(konyvSzam);
The variable gets the value but after I check it doesn't work
Mostly our DB call works asynchrous in node js and JavaScript try to enclose db calls in promise or use async/await .
This question already has answers here:
What are the rules for JavaScript's automatic semicolon insertion (ASI)?
(7 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
I started learning async iterators and could not figure how to store the results in global variables to use them later on.
let arr = [];
(async () => {
for await (let item of items) {
console.log(item);
// arr.push(item) HOW DO I PUSH THAT INSIDE?
}
})()
I declared Symbol.asyncIterator method on the object. Please check codepen https://codepen.io/INR4GE/pen/XWbVxoy?editors=1111 . Dunno why it errors on line 13, in browser it works fine. Please don't ask why am i even doing it this way. It is just for learning purposes