2

I tried to use server side method to set a session variable.

Meteor.methods({
'countvisit':function(){
  var count=mydict;
  count=count+1;
  console.log("another visit come. Totally "+ count.toString()+" view the site");
  mydict=count;
  Session.set("globalcount", mydict);
}
});

The error happens on Session.set, I find I can not get the Session on serverside. the error is:

I20160224-11:34:38.773(11)? another visit come. Totally 1 view the site
I20160224-11:34:38.774(11)? Exception while invoking method 'countvisit' ReferenceError: Session is not defined
I20160224-11:34:38.774(11)?     at [object Object].Meteor.methods.countvisit (server/startup.js:12:7)
I20160224-11:34:38.774(11)?     at maybeAuditArgumentChecks (livedata_server.js:1698:12)
I20160224-11:34:38.774(11)?     at livedata_server.js:708:19
I20160224-11:34:38.774(11)?     at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
I20160224-11:34:38.774(11)?     at livedata_server.js:706:40
I20160224-11:34:38.775(11)?     at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
I20160224-11:34:38.775(11)?     at livedata_server.js:704:46
I20160224-11:34:38.775(11)?     at tryCallTwo (C:\Users\sheng\AppData\Local\.meteor\packages\promise\0.5.1\npm\node_modules\meteor-promise\node_modules\promise\lib\core.js:45:5)
I20160224-11:34:38.775(11)?     at doResolve (C:\Users\sheng\AppData\Local\.meteor\packages\promise\0.5.1\npm\node_modules\meteor-promise\node_modules\promise\lib\core.js:171:13)
I20160224-11:34:38.775(11)?     at new Promise (C:\Users\sheng\AppData\Local\.meteor\packages\promise\0.5.1\npm\node_modules\meteor-promise\node_modules\promise\lib\core.js:65:3)

How can I set session variables in Meteor.methods() on the server?

Michel Floyd
  • 18,793
  • 4
  • 24
  • 39
user504909
  • 9,119
  • 12
  • 60
  • 109
  • 1
    You don't because Session variables are only accessible on a single client instance. You'll need to use pub/sub to share the data across instances - e.g. create a collection called "visits" and modify it as needed. If you are looking to count the current number of connected clients, see [this question](https://stackoverflow.com/questions/25643114/how-can-i-display-a-list-of-all-logged-in-users-with-meteor-js). – David Weldon Feb 24 '16 at 01:12

1 Answers1

1

look like you are recording the page view on both client and server. The best way is actually saving it into mongo and publish it back to client

Community
  • 1
  • 1
Thai Tran
  • 9,815
  • 7
  • 43
  • 64
  • 1
    Global variables are not shared between client and server. Also they don't persist across server restarts either. – Michel Floyd Feb 24 '16 at 01:01
  • @MichelFloyd That is why i wrote the `update`. In his question, he only mentioned about sharing the global variable on the server – Thai Tran Feb 24 '16 at 01:03
  • But even a global variable wouldn't solve the problem as each user would have a different value for it. – Michel Floyd Feb 24 '16 at 01:13
  • @MichelFloyd the first time i read the question, i thought he is trying to share the data on the server side, that is why i suggested him to use the global variable. Then i read the session name in the code and it is about the total view of the page, which lead to sharing the data on both client and server across user. That is why i made the update and ask him to save it into the mongo. The first answer is not correct anymore but i don't want to remove it because it gonna look like i gonna change the whole answer through the editing, which is not good in my opinion. You know what i mean ? – Thai Tran Feb 24 '16 at 01:20
  • IMO it's better to edit the answer down to a solution that's going to work for the OP and delete mis-steps. – Michel Floyd Feb 24 '16 at 01:30