Why is createReadStream not updating a variable? [duplicate] - javascript

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.

Related

Trouble returning value from an Axiois get request React App [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)
Closed 6 months ago.
I am fairly new to web dev/react/js but I have been trying to request data from the backend using this code, and I can't seem to return the correct value. In the code below, it still returns 10 rather than setting the "hey" variable to the JSON data I am looking for. I'm a little out of my depth but have been searching for solutions, any help welcome!
function getViews() {
let hey = 10;
axios.get("http://localhost:8000/views")
.then((response) => {
hey = JSON.stringify(response)
return hey
//This return statement doesn't get called
})
.catch((error) => {
console.error(error);
})
return hey
}
enter image description here

Different logs for the same global variable (Node.js) [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)
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?

Javascript/Node.js - How to assign the output from a node-fetch to a var (Node-Fetch API) [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 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.

How do I access data outside of the then scope when resolving a promise? [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)
Closed 3 years ago.
Noob question here. Can someone elaborate on why seriesData variable outside the then scope is still an empty object. Additionally, what's the appropriate way of accessing data outside of said scope?
fetchData is a method that returns an array of three objects when successfully resolved.
Obviously, the console.log inside the then scope prints the expected seriesData object with the resulting data from the resolved promise.
Here's the code.
import fetchData from './data';
const seriesData = {};
fetchData()
.then(res => {
seriesData.characters = res[0];
seriesData.locations = res[1];
seriesData.episodes = res[2];
console.log(seriesData);
})
.catch(error => console.error(error.message));
console.log(seriesData);

JavaScript Array result disappear [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 6 years ago.
I'm writing a function that pass in a string called "term" to search in my MongoDB, then add its results to an existed empty array of results called "result[]":
var searchAndAddToResults = (result, term)=> {
Place.find({ $or: [
{name: term}, {category: term}
] }, places=> {
for (let i in places) {
if (!itemExists(result, places[i].toObject())) {
result.push(places[i].toObject())
}
}
console.log(result) // result isn't empty, which is good
})
console.log(result) // result is empty, which is bad and weird
return result // this returned result is also empty, THIS is the real problem
}
Can anyone help me with restructuring this code to get it work? Thanks
These calls are asynchronous - which is why you are getting results in succcess callback of Place.find() and finding one outside of the method. Because as soon as Place.find() is called the next line is executed. So it'll be great if you learn to work with these asynchronous calls of javascript.

Categories