0

So I'm calling a method from the client that has a callback:

Meteor.call("readHeaders", Meteor.user().emails[0].address+'/'+Session.get("file1"), 
            function(err,result){
                console.log(result);
        });

and here is the method that's being called:

readHeaders: function(fileName){
        var nodeFS = Meteor.npmRequire('node-fs');
        nodeFS.readFile("somepath/"+fileName,'utf8', function read(err, data){
            if (err) {
                throw err;
            }
            var headers = [data.slice(0,data.indexOf('\n')).split(",")];

            return headers;

        });
    }

The correct result gets printed on the server, but on the client it returns undefined. Any suggestions?

ray smith
  • 380
  • 3
  • 14
  • Have you tried logging the err in console on client to see what it is? You might need to use Meteor.bindEnvironment on the readFile callback since you are making an async call to an external node library. – danSiebes Sep 04 '15 at 16:14
  • http://stackoverflow.com/questions/12569712/meteor-calling-an-asynchronous-function-inside-a-meteor-method-and-returning-th/21542356#21542356 – Brian Shamblen Sep 04 '15 at 16:15
  • @BrianShamblen He's got a callback, this doesn't appear to be an issue of asynchronocity. – ffxsam Sep 04 '15 at 23:25
  • You're returning the headers value inside the callback of the fs function. At that point the meteor method has already returned. Look into fibers or wrapAsync. – Brian Shamblen Sep 05 '15 at 06:22

1 Answers1

0

I'm guessing your method call and callback are fine, but your method itself is probably not returning what you're expecting. Add a console.log(headers) before the return headers line and make sure it's an object.

ffxsam
  • 26,428
  • 32
  • 94
  • 144