Sailsjs 0.10.3 - Heroku - RedisToGo - req.session undefined

548 Views Asked by At

When using sailsjs v0.10.3 with Redis To Go for session storage, req.session is always undefined.

It is undefined when I deploy both locally and to Heroku. req.session is correctly defined when I use the default memory adapter.

I created a sailsjs app:

sails new testapp
sails generate api test testSet testGet

Installed connect-redis v1.4.7:

npm install connect-redis@~1.4.7

Set the configuration in config/session.js:

adapter: 'redis',
host: 'hoki.redistogo.com',
port: 10015,
db: 'redistogo',
pass: '88819aa089d3dd86235f9fad4cb92e48'

Set the configuration in config/socket.js:

adapter: 'redis',
host: 'hoki.redistogo.com',
port: 10015,
db: 'redistogo',
pass: '88819aa089d3dd86235f9fad4cb92e48'

Created some controller actions which get and set a session value:

UserController.js

testSet: function (req, res) {

  req.session.testVar = "I am the test var!";

  return res.ok();
},

testGet: function (req, res) {
  return res.json({
    testVar: req.session.testVar
  });
}

And finally deployed to Heroku:

git init
git add .
git commit -m "Initial commit"
heroku create
heroku addons:add redistogo
git push heroku master
git 

This is the error:

error: Sending 500 ("Server Error") response:  
TypeError: Cannot set property 'testVar' of undefined 
at module.exports.testSet (/app/api/controllers/TestController.js:46:25)

It seems like this simple example should work.

Here is a repo of the example above:

https://github.com/derekbasch/sailsjs-redistogo-testapp

Does anyone know what I am doing wrong?

UPDATE:

I tried using the MemoryStore adapter on Heroku to get/set a session variable. That failed with undefined also. It works locally. Now I am even more confused.

1

There are 1 best solutions below

1
On

We are using rediscloud (sails app on heroku) and the db property is set to 0. Could this be the problem?

Also, you should parse the URL provided by heroku via en env variables. This is what we use (coffeescript):

parseRedisUrl = ( url ) ->
  parsed = require( 'url' ).parse( url )
  password = (parsed.auth || '').split( ':' )[1]

  hostname: parsed.hostname
  port: parsed.port
  password: password

redis = parseRedisUrl( process.env.REDISCLOUD_URL || "redis://localhost:6379" )

module.exports.session =

  secret: '...'

  adapter: 'redis'
  host: redis.hostname
  port: redis.port
  pass: redis.password
  db: 0
  ttl: 60 * 60 * 24