How to make this nodeJs code synchronous using Meteor.wrapAsync?

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.

Haven’t had a look at your code in detail but just a quick heads up in case you missed it: you aren’t returning anything from your readFile method which would explain it being undefined on the client.

Also, to me, there doesn’t seem much benefit in declaring your readFile on both the client and server as it only ever does anything useful on the server. Unless I’ve missed something!

Hope that’s some help :smile: