Trouble with getting the session information #175

Open
rahulkgupta opened this Issue Jan 25, 2012 · 9 comments

Comments

Projects
None yet
6 participants
@rahulkgupta

Hey I'm trying to get the session information with now, which I learned is possible under v 0.8.
The changelog says that it should be simple, so I initialized my server like so.

var everyone = nowjs.initialize(app, {cookieKey:'mySecret'});

Then in an everyone.now function, I do console.log(this.user.session) but I get Cannot read property 'session' of undefined

I'm sure I'm forgetting to do something.
Any help would be very much appreciated.

@DungFu

This comment has been minimized.

Show comment Hide comment
@DungFu

DungFu Jan 25, 2012

Contributor

I have the same problem. I attempted to initialize like this:

var app =  express.createServer(
    express.cookieParser(),
    express.session({ secret: 'mySecret' })
);

But this does not seem to work either, perhaps it needs to be combined with how you did it above.

Contributor

DungFu commented Jan 25, 2012

I have the same problem. I attempted to initialize like this:

var app =  express.createServer(
    express.cookieParser(),
    express.session({ secret: 'mySecret' })
);

But this does not seem to work either, perhaps it needs to be combined with how you did it above.

@rahulkgupta

This comment has been minimized.

Show comment Hide comment
@rahulkgupta

rahulkgupta Jan 25, 2012

I did that as well. :/ No luck.

I did that as well. :/ No luck.

@ericz

This comment has been minimized.

Show comment Hide comment
@ericz

ericz Feb 1, 2012

Contributor

hey rahulkgupta,

cookieKey doesn't refer to your secret, but rather the the name of the cookie if you had changed to from the default of connect.sid.

Otherwise, we are exploring reports of sessions not working properly so look for a fixed release soon.

Contributor

ericz commented Feb 1, 2012

hey rahulkgupta,

cookieKey doesn't refer to your secret, but rather the the name of the cookie if you had changed to from the default of connect.sid.

Otherwise, we are exploring reports of sessions not working properly so look for a fixed release soon.

@rahulkgupta

This comment has been minimized.

Show comment Hide comment
@rahulkgupta

rahulkgupta Feb 1, 2012

Oops actually I read @DungFu 's comment incorrectly. I set the cookiekey to the key value in express.session.
I just recently got it to work. I don't know how I fixed it, but I suspect it has to do w/ me maybe making the mistake of using the this variable in a callback within a now function. Regardless, it works like a charm now. Thanks!

Oops actually I read @DungFu 's comment incorrectly. I set the cookiekey to the key value in express.session.
I just recently got it to work. I don't know how I fixed it, but I suspect it has to do w/ me maybe making the mistake of using the this variable in a callback within a now function. Regardless, it works like a charm now. Thanks!

@zachaller

This comment has been minimized.

Show comment Hide comment
@zachaller

zachaller Feb 15, 2012

@ericz any words on if there was and issue, i seem to be having some troubles not sure if its something i am doing or not

@ericz any words on if there was and issue, i seem to be having some troubles not sure if its something i am doing or not

@rahulkgupta

This comment has been minimized.

Show comment Hide comment
@rahulkgupta

rahulkgupta Feb 15, 2012

This is what I am doing and it is working.

app.use(express.session({ secret: "keyboard cat" , key : 'myKey'}));

var everyone = nowjs.initialize(app, {cookieKey:'myKey'});`

and the now functions are defined in app.
Furthermore, make sure that you are not using this.user.session in some callback. I think that won't work.
So, the first thing I did in my now function was
var userId = this.user.session.userId;

This is what I am doing and it is working.

app.use(express.session({ secret: "keyboard cat" , key : 'myKey'}));

var everyone = nowjs.initialize(app, {cookieKey:'myKey'});`

and the now functions are defined in app.
Furthermore, make sure that you are not using this.user.session in some callback. I think that won't work.
So, the first thing I did in my now function was
var userId = this.user.session.userId;

@steveWang

This comment has been minimized.

Show comment Hide comment
@steveWang

steveWang Feb 19, 2012

Contributor

@rahulkgupta: see the rest of this comment for reasoning as to why you can't reference this.user.session in a callback.

Javascript "fun fact": this refers to the context in which a function was called. Your callbacks are called in a different context than the original function, so they rebind the this variable.

In order to use this in a callback, you must take advantage of closures. This is often referred to as the that trick: basically, just create a local variable (e.g. var self = this;) and use self in your callbacks instead of this.

Example:

everyone.now.ping = function () {
  var self = this;
  this.now.pong(function () {
    console.log(self.user.id, "acknowledged.");
  });
};
Contributor

steveWang commented Feb 19, 2012

@rahulkgupta: see the rest of this comment for reasoning as to why you can't reference this.user.session in a callback.

Javascript "fun fact": this refers to the context in which a function was called. Your callbacks are called in a different context than the original function, so they rebind the this variable.

In order to use this in a callback, you must take advantage of closures. This is often referred to as the that trick: basically, just create a local variable (e.g. var self = this;) and use self in your callbacks instead of this.

Example:

everyone.now.ping = function () {
  var self = this;
  this.now.pong(function () {
    console.log(self.user.id, "acknowledged.");
  });
};
@rahulkgupta

This comment has been minimized.

Show comment Hide comment
@rahulkgupta

rahulkgupta Feb 19, 2012

Yea that's what I'm doing.

Yea that's what I'm doing.

@dre1080

This comment has been minimized.

Show comment Hide comment
@dre1080

dre1080 Feb 20, 2012

I'm also having this same issue as @rahulkgupta

dre1080 commented Feb 20, 2012

I'm also having this same issue as @rahulkgupta

@ghost ghost referenced this issue Mar 9, 2012

Open

nowjs.sessions is empty #184

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment