简体   繁体   中英

Callback in client for Meteor method call returning undefined

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM