A better way the get a string from server


Hey all, first post here.

Since Meteor is the first Framework I’ve ever tried to tackle (so bear with me). I gave myself the simple challenge to take a string from the server, and post it into html. Simple right?

Well after a lot of tears, cookies, more tears and reading. I finally found a solution that looks like this:

// Main.js (server) 
    getMyString: function() {
      return "Aloha Mundo";
// Main.js (client)
	if (Meteor.isClient) {
 		Session.setDefault("myString", null);
  		Meteor.call("getMyString", function(err, res) {Session.set("myString", res);});
  		var printthedamnthing = Session.get('myString');
		return "The princess yells " + printthedamnthing  + " and chugs a pint! Hooray";


Well, it works. But is looks verbose, and I needed to add Sessions. So my question is - is there a more elegant solution?

Thanks :slight_smile:


So, you want to get some data from the server using methods. That’s fine, but because methods are called on-demand (in contrast to subscriptions and publications which fetches data automatically) you need to design your interface around that. This also makes everything more verbose.

Your server code is fine. As for the client code, I think a better place for fetching your data is in the onCreated event and also use reactive-var package instead of session:

  this.myString = new ReactiveVar("");
  Meteor.call("getMyString", (err, res) => {

  output: () => Template.instance().myString.get()

That should do the trick. Note that it’s not really less verbose :wink:


Thanks :slight_smile:

Nice to see that it can be dome without sessions. So that’s at least a step up.

I’ll try to look into subscriptions/publications a bit later on, since I’m still struggling with a lot of basic stuff. And I’m trying to stay clear of Mongo for now. It’s not that I’m building anything particular at the moment. I’m just trying to get to know Meteor by giving myself simple tasks, I just thought this was a lot of code for a simple “Hello World”.

Again, thanks again for your input. :+1:


It’s not really a simple hello world. In a more traditional architecture it’s more like requesting data from a REST endpoint using AJAX - and I think you’ll find writing the client and server components of that a lot more verbose! :wink:


You could use this package to reduce the code even more: meteor add simple:reactive-method.

With it it’s as simple as:

// client.js
  output() {
    return ReactiveMethod.call("getMyString");