简体   繁体   中英

Meteorjs send variable from server to client

So I'm trying to send some data from server to client. Here is my client code that calls a method:

 'change #yes' : function (event){

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

and here is the method that is being called:

readHeaders: function(fileName){

        Future = Npm.require('fibers/future');
        var myFuture = new Future();
        var nodeFS = Meteor.npmRequire('node-fs');
        nodeFS.readFile("/Users/ray/Desktop/juju/upload/"+fileName,'utf8', function read(err, data){
            if (err) {
                throw err;
            }
            var headers = data.slice(0,data.indexOf('\n')).split(",");

            myFuture.return(headers);

        });
        myFuture.wait();
    }

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

            console.log(headers);
            return headers;

        });
}

Here I'm using fibers, but I have also tried to use wrapAsync, to no avail. Does anyone know how to pass a variable from the server to the client in Meteor?

You could resort to a plain rest API if you so chose to.

WebApp.connectHandlers.use('/file', function (req, res, next) {
  var filename = req.body.filename;
  res.write(fs.readFileSync(path.join(__dirname + '/' + filename));
  res.end();
});

Then on the client something like

HTTP.get('/file', { filename: 'file1.txt' }, function () {
  console.log('finished')
});

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