2
votes

So, I was attempting to do something like the following:

if(Meteor.isServer){
    Meteor.methods({connect_to_api: function(vars){
        // get data from remote API
        return data;
    }});
}

if(Meteor.isClient){
    Template.myTpl.content = function(){
        Meteor.call('connect_to_api', vars, function(err,data){
            Session.set('placeholder', data);
        });
        return Session.get('placeholder');
    };
}

This seemed to be working fine, but, of course, now breaks in 0.5.9 as the Session object has been removed from the server. How in the world do you now create a reactive Template that uses a server-only (stuff we don't want loading on the client) method call and get data back from that Method call. You can't put any Session references in the callback function because it doesn't exist on the server, and I don't know of any other reactive data sources available for this scenario.

I'm pretty new to Meteor, so I'm really trying to pin down best-practices stuff that has the best chance of being future-proof. Apparently the above implementation was not it.

EDIT: To clarify, this is not a problem of when I'm returning from the Template function. This is a problem of Session existing on the server. The above code will generate the following error message on the server:

Exception while invoking method 'connect_to_api' ReferenceError: Session is not defined
at Meteor.methods.connect_to_api (path/to/file.js:#:#)
at _.extend.protocol_handlers.method.exception ... etc etc
2
I think your code would not work too well anyway as the return Session.get('placeholder'); would happen before the callback on the Meteor.call is completed, no? I would have thought that setting Session in the callback would still work but if it doesn't, I agree that it could be an issue. - jtblin
Setting Session in the callback doesn't work. The callback is not run on the client, but is passed to the method (on the server) to run on method completion. With Session access removed from the server, I'm having a hard time conceptualizing a way to set client-specific data from the server without using MongoDB--I don't understand why it was removed... Again, if I have "stinkin' thinkin'" I'm hoping someone can educate me on the "right" (or at least, better) way to do this. - ValZho
It works fine, I've just made a quick example to prove it, see below. The issues are with your code. - jtblin

2 Answers

1
votes

Setting the session in the callback seems to work fine, see this project I created on github: https://github.com/jtblin/meteor_session_test. In this example, I return data in a server method, and set it in the session in the callback.

There are 2 issues with your code:

1) Missing closing brace placement in Meteor.methods. The code should be:

Meteor.methods({
    connect_to_api: function(vars) {
        // get data from remote API
        return data;
    }
});

2) As explained above, you return the value in the session, before the callback is completed, i.e. before the callback method had the time to set the session variable. I guess this is why you don't see any data in the session variable yet.

0
votes

I feel like an idiot (not the first time, not the last). Thanks to jtblin for showing me that Session.set does indeed work in the callback, I went back and scoured my Meteor.method function. Turns out there was one spot buried in the code where I was using Session.get which was what was throwing the error. Once I passed that value in from the client rather than trying to get it in the method itself, all was right with the world.

Oh, and you can indeed order things as above without issue.