I believe I am having an Ansync issue in my code. I keep getting an "undefined" value. I have been trying to make adjustments based on this post but haven't had any success. Here is my current code where I can console.log() the value from within the fs.readFile
var fs = require('fs');
var csv = require("csv");
// *** START HERE ***
fs.readFile("contacts.csv","utf8", function(err, data){
csv.parse(data, {columns:true}, function(err, parseData){
console.log(parseData); // LOGS THE DATA
})
});
Ideally, I would like to have this 'parseData' array globally. I tried reading some of the suggestions from the link above and I get an "undefined" value. Here is my most recent attempt.
var fs = require('fs');
var csv = require("csv");
// *** START HERE ***
var friendsList; // define var in global space
function friendsListData(callback){
fs.readFile("contacts.csv","utf8", function(err, data){
csv.parse(data, {columns:true}, function(err, parseData){
friendsList = parseData;
callback();
})
});
}
function returnFriendLists(){
console.log(friendsList);
}
friendsListData(returnFriendLists);
This works, it console.log's the value. But this isn't what I want, I want the actual value so I can use it later, I would like it in a global variable. Is there any way of doing this? I tried changing my returnFriendLists function to:
function returnFriendLists(){
return friendsList;
}
But still receive "undefined" when I console.log(friendsList) globally.