Suppose I've file named orders.js
something like this:
exports.getOrders = function() {
return {'a': 'b'};
}
and another file named report.js
like this:
var Order = require('./src/orders');
console.log(Order.getOrders());
This will print {'a' : 'b'}
and I'm happy. Now I want to make things more complicated, since I need to read data from a CSV (using fast-csv node package), so I modified the orders.js
:
var csv = require('fast-csv');
exports.getOrders = function(clientId) {
var orders = [];
csv
.fromPath("./data/orders.csv", {headers: true, delimiter:';'})
.on("data", function(data) {
orders.push({'price' : data.price});
}
})
.on("end", function(){
console.log(orders) // THIS WILL PRINT THE CORRECT ARRAY
return orders;
});
console.log(orders); // THIS WILL PRINT AN EMPTY ARRAY
return orders;
}
How can I return the correct array when Order.getOrders()
is invoked in report.js
?