0

I am trying to access session data from node.js that is stored in redis. In the redis-cli I ran Keys * and returned

1) "sess:ZRhBJSVLjbNMc-qJptjiSjp8lQhXGGBb"
2) "sess:6p1EcGvJukTT26h88NqoTGdz2R4zr_7k"

If I then run GET I get back what looks like a hash

redis 127.0.0.1:6379> GET sess:ZRhBJSVLjbNMc-qJptjiSjp8lQhXGGBb
"{cookie:{originalMaxAge:null,expires:null,httpOnly:true,path:/},userKey:a92ca307-b315-44bc-aadf-da838d063c78,
authenticated:true,clientKey:1ccb5308-2a7e-4f49-bcdf-b2379de44541}"

If I try to get the value in userKey using

hget sess:oq6RW1zP7sfcZZc4wb1RHbti390FxL7- userKey

it returns

ERR Operation against a key holding the wrong kind of value

so I ran a TYPE check and found that it's not a hash but a string. I am a bit confused now as the whole thing looks like a hash and I cannot figure out how to return the values that I need as just calling get returns the whole thing.

Is there some other command I need to use to get the values?

Thanks

Simon245
  • 178
  • 2
  • 19

2 Answers2

2

If you can GET aganist the key then it is not a hash because you would get ERR Operation against a key holding the wrong kind of value. And it was confirmed by yourserlf doing HGET and getting the error.

Probably that keys looks like a hash because (it is a hash but not redis hash datatype) it is the unique token that was issued to user in his session cookie in client. When user send this cookie to the server in every request the server can retrieve session info from redis using the cookie value as redis key.

The value is a string in JSON format. You have to retrieve the whole value and parse it; ussing JSON.parse in node.js could do the job. Once the value is parsed you have a JavaScript object which attributes can be access in standard way:

var sessionData = JSON.parse(JSONString);
console.log(sessionData.userKey)
jlvaquero
  • 8,571
  • 1
  • 29
  • 45
  • Thank you, as soon as I read it's a string in json format I smacked myself in the face. Cheers for taking the time to give such a full answer – Simon245 Jan 15 '16 at 08:00
1
  1. It's a string
  2. You can't get some session value directly, because it's serialized to some format(in this case, JSON)
  3. If the session is written by node.js, you should use the same API to read.
  4. If the session is written by other system, and you must parse it with node, you should just GET it, and json parse it(JSON.parse)
Nowhy
  • 2,894
  • 1
  • 16
  • 13