0

I am seeking an efficient way to achieve my flow of events. So far this is what I am trying to do:

I have an object that is the result of a Meteor.call(...function, (error, result)) I declare the object on my client myObject = result This call takes place when a user clicks a button

Now, how can I put this variable into another meteor call that I am now making within a Router.route (Iron router)? As of now, it is undefined.

The values within this object are already defined on the server within a function. I am really passing this object from the server,to the client, and back around to the server again. Can I store it on the server to be used in other function?

I read that storing global variables on the server can mess things up for other user when they set values to these variables. Is this correct?

e.g. the next call but the myObject is undefined

Router.route('/home/', {
name: 'home',
waitOn: function () {
 },
action: function () {
// putting 'myObject' into call to pass onto the server
    Meteor.call('toUseTheObjectOnServer', myObject, function (error, response) {
        if (error) {
            console.log(error)
        } else {
            console.log(response)
        }
    })
    }
 if (this.ready())
 this.render('home');
 else
 this.render('Loading');
}
});

my flow of events to achieve:

  1. (server) http post - I have 2 fields (token, token_secret) returned within the body. callback returns the result to the client

  2. (client) I use one of the fields 'token' to construct a url and direct the user to this as soon as I get the result. The user signs into the external app, effectively granting me access to their data.

If the user grants me access, the user is directed to the callback url I have provided to the service provider. The service provider adds a parameter to this callback url which indicates if the user granted me access. The parameter in the callback url is authorize=1...this means i have been granted. authorize=0... I have not been granted access.

  1. (server) If I have been granted access, I then need to make another http call on server using the token and token_secret values from step 1. which are now 'authorised' values. The response in the body are 'access tokens' that I store in my database in order to make authorised api calls for whatever data I want.
3
  • Just to make this clear: You have data present on the server that you want to send to a short trip to the client before getting it back unmodified and doing stuff with it? I have a hard time wrapping my head around your structure. Could you try explaining the context of this question, what kind of feature you are trying to achieve? Then we may provide help in managing the data you're manipulating. Commented Feb 19, 2015 at 18:14
  • @Kyll, please see the flow of events I added to my question. Commented Feb 20, 2015 at 11:46
  • After my tokens have been authorised, the three functions I would like to perform are in the following link stackoverflow.com/questions/28571434/… Commented Feb 20, 2015 at 11:51

1 Answer 1

1

In order to use it between files (set on a template, and used in your router), you'll need to store it in some kind of global state. In this example, I'll use a session variable:

Template.myTemplate.events({
  'click button': function() {
    Meteor.call('methodToGetObject', function(err, response) {
      Session.set('myGlobalObject', response);
    });
  }
});

Router.route('/home', {
  action: function() {
    var object = Session.get('myGlobalObject');
    Meteor.call('toUseTheObjectOnServer', object, function(err, response) {
      console.log(response);
    });
  }
});

You could store the value in memory for each user on the server, but that isn't ideal for two reasons:

  • If the server restarts you will lose the state for all users.
  • This doesn't work if you scale past a single server.

A more robust solution is to store the state in the database and auto-publish it for each connected client. This approach may make sense if the user should automatically receive the object after reconnecting. We can explore that idea more if the first solution doesn't work for you.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi David, what you have showed makes sense with regards to my original question. So thanks you. However, I have realised I am not using the most efficient method to achieve what I need to and have modified my question. Plus point for solving what I thought I needed originally.
If I get the variable by "var object = Session.get('myGlobalObject')" within my Meteor.call, it prints out. But if I declare "var object = Session.get('myGlobalObject')" in the route, it does not get declared
This is because my user is being directed to a url that is not my own and then returning, i.e. the page is being refreshed and so the session is deleted
Given that you need the data across sessions, it seems like you should just store it on the user's document, either as part of the profile, or as a separate field.
Thats what I ended up doing. Its easier and will be stable in the long run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.