Cant use sessions with custom session model
WhoIsDT opened this issue · comments
Just for testing, i've decided to create custom table 'app_sessions'. So here is a definition from docs example:
const Sequelize = require('sequelize')
const sequelize = require('../../config/database')
const Session = sequelize.define('app_sessions', {
sid: {
type: Sequelize.STRING,
primaryKey: true,
},
expires: Sequelize.DATE,
data: Sequelize.STRING(50000),
})
const extendDefaultFields = (defaults, session) => ({
data: defaults.data,
expires: defaults.expires,
})
module.exports = { Session, extendDefaultFields }
And here is my middleware:
app.use(
session({
secret: 'test secret',
store: new SequelizeStore({
db: sequelize,
table: 'Session',
checkExpirationInterval: 24 * 60 * 60 * 1000,
expiration: 14 * 86400,
extendDefaultFields,
}),
// resave: false,
// saveUninitialized: false,
// cookie: { maxAge: 14 * 86400 },
})
)
And then, i'm trying to use sessions for example in index controller:
exports.getIndexPage = async (req, res) => {
const { categories, navCategories } = req.body
req.session.test = 'h1'
const featuredCategories = getFeaturedCategories(categories)
const slider = await getSlider()
const productsNewArrivals = await getProductsNewArrivals(categories)
res.render('index.pug', { navCategories, slider, featuredCategories, productsNewArrivals })
}
But i'm getting error:
TypeError: Cannot read property 'findCreateFind' of undefined
at SequelizeStore.set (/Users/isee/Work/Web/Projects/diantus/node_modules/connect-session-sequelize/lib/connect-session-sequelize.js:87:10)
at Session.save (/Users/isee/Work/Web/Projects/diantus/node_modules/express-session/session/session.js:72:25)
at Session.save (/Users/isee/Work/Web/Projects/diantus/node_modules/express-session/index.js:402:15)
at ServerResponse.end (/Users/isee/Work/Web/Projects/diantus/node_modules/express-session/index.js:331:21)
at ServerResponse.send (/Users/isee/Work/Web/Projects/diantus/node_modules/express/lib/response.js:221:10)
at done (/Users/isee/Work/Web/Projects/diantus/node_modules/express/lib/response.js:1008:10)
at Object.exports.renderFile (/Users/isee/Work/Web/Projects/diantus/node_modules/pug/lib/index.js:421:12)
at View.exports.__express [as engine] (/Users/isee/Work/Web/Projects/diantus/node_modules/pug/lib/index.js:464:11)
at View.render (/Users/isee/Work/Web/Projects/diantus/node_modules/express/lib/view.js:135:8)
at tryRender (/Users/isee/Work/Web/Projects/diantus/node_modules/express/lib/application.js:640:10)
at Function.render (/Users/isee/Work/Web/Projects/diantus/node_modules/express/lib/application.js:592:3)
at ServerResponse.render (/Users/isee/Work/Web/Projects/diantus/node_modules/express/lib/response.js:1012:7)
at exports.getIndexPage (/Users/isee/Work/Web/Projects/diantus/src/controllers/index.js:14:7)
And if i wount use custom model, and everything will leave as it is, except table prop in store, i'll have fully working sessions, with custom table in db 'Sessions'.
So what im doing wrong. or there is some other problems?
Hi,
the table
property you supply to the SequelizeStore
constructor needs to be the name of the table you provide. You copied from the README the whole setup, but changed the table name only in sequelize.define
, not in the table
property.
I setup a repository with a very basic connect-session-sequelize example and in a branch replicated your code. This change fixes your issue: mweibel/connect-session-sequelize-example@a6dd21e
How I debugged this:
- Adjust https://github.com/mweibel/connect-session-sequelize-example in a branch to have your code
- install all deps using
npm install
- run
npm start
and see the error - open
node_modules/connect-session-sequelize/lib/connect-session-sequelize.js
and find relevant code on line 44 - add
console.log(options.table, options.db, options.db.models)
before that line and re-run the app - saw that the
options.db.models
object containsapp_sessions
and notSession
- was able to fix the bug
⭐ don't be afraid of digging into your node dependencies and learn the code or adjust the code to debug stuff ⭐
Please feel free to open a PR with a change to the README to make this more clear for further users.
- Michael