I have some nodeJs code on my server that turns a cvs file into a JSON object. I’m trying to figure out how to utilize wrapAsync with my code, but I’m not too sure how to. Here is my attempt at it:
readFile: function(fileName){
var readFileToJsonSync = Meteor.wrapAsync(readFileToJson);
var result = readFileToJsonSync(fileName);
console.log(result);
},
});
if (Meteor.isServer){
var readFileToJson = function(fileName, cb){
var nodeFS = Meteor.npmRequire('node-fs');
var Converter = Meteor.npmRequire("csvtojson").Converter;
var fileStream = nodeFS.createReadStream("/Users/ray/Desktop/juju/upload/"+fileName,'utf8');
//new converter instance
var converter = new Converter({constructResult:true});
//end_parsed will be emitted once parsing finished
converter.on("end_parsed", function (jsonObj) {
return jsonObj ; //here is your result json object
});
//read from file
fileStream.pipe(converter);
}
}
And in the client:
Meteor.call("readFile", Meteor.user().emails[0].address+'/'+Session.get("file1"), function (error, result){
console.log(result);
});
When I run this code asynchronously, the proper output gets printed in the terminal, but undefined is printed out in the client console. This current attempt prints out nothing in both the server and client.