Reputation: 59
I have this code here
function marketInfo() {
var market_data = [];
csv
.fromPath("market_info.csv")
.on("data", function(data) {
if (data.length != 0) {
market_data.push(data);
}
})
.on("end", function() {
for (var i=0; i < market_data.length; i++) {
result.push(market_data[i]);
}
// console.log(market_data);
return market_data;
})
}
It prints the data and I know someone mentioned something about callbacks in another post, but I don't quite understand... The data has 0 headers so it's inline with [['market_cap', '123456789']] and so on. When returning the array, it's undefined. I tried to search but I can't figure out whats the issue here
Upvotes: 1
Views: 104
Reputation: 211740
Your return market_data
function just yells into the void. It does nothing meaningful. The marketInfo
function itself makes use of asynchronous code with callbacks. Unless you supply a callback to that function you have no way of capturing that return value.
function marketInfo(cb) {
var market_data = [];
csv
.fromPath("market_info.csv")
.on("data", function(data) {
if (data.length != 0) {
market_data.push(data);
}
})
.on("end", function() {
for (var i=0; i < market_data.length; i++) {
result.push(market_data[i]);
}
// console.log(market_data);
cb(null, market_data);
})
}
Where your callback needs to be passed in:
marketInfo(function(err, market_data) {
// Do stuff with data
});
As writing callback-driven code can be harrowing you might try and steer towards solutions based around either Promises or async
/await
instead.
Upvotes: 1