kadirahq / flow-router

Carefully Designed Client Side Router for Meteor
MIT License
1.09k stars 195 forks source link

SSR + Session #583

Closed blntylmn closed 8 years ago

blntylmn commented 8 years ago

I'm getting this error when I try to

import { Session } from 'meteor/session'

Error: Can't find npm module 'meteor/session'. Did you forget to call 'Npm.depends' i
n package.js within the 'modules-runtime' package?

Is there a way to use Session with SSR ?

kokjinsam commented 8 years ago

If you're using React, you can do:

componentDidMount() {
  const Session = require('meteor/session');
  // Do whatever you want with Session
}
blntylmn commented 8 years ago

I tried it but it complaint about there is no set function of Session

Session.set('test','something');

TypeError: Session.set is not a function

kokjinsam commented 8 years ago

Try console log Session. You will see that Session is an object not a func. You can try Session.Session.set('test', 'something').

blntylmn commented 8 years ago

Thanks sammkj it works. But I feel something is not right Everytime when I set a session variable I have to use this syntax : const rv = require('meteor/session'); rv.Session.set('test','something'); and when get it this syntax : const rv = require('meteor/session'); let variable=rv.Session.get('test'); //use variable Must be a simple way but I am newbie in react :( Could you suggest a proper and shorter way ?

kokjinsam commented 8 years ago

I would create a helper function like:

function setSession(stuff) {
  const Session = require('meteor/session').Session;
  Session.set(stuff);
}

Then call that helper function anywhere in your react component. Hope that helps!

blntylmn commented 8 years ago

Yeap. Sometimes when you think complex you cab boot see the simple solutions :) Thats it. Thanks..