Home > Blockchain >  Table '[database-name].sessions' doesn't exist - using express-session
Table '[database-name].sessions' doesn't exist - using express-session

Time:10-14

Here is some sample code that outlines my issue. I'm trying to get express-session / connect-session-sequelize to work for a website with login functionalities.

However, when I try to call my POST request, I get the following error:

image I can only assume it's trying to store session data onto my database, but cannot find a table. I can bypass this by going in and creating the table manually with all the columns it wants, but I'm wondering if there's an issue in my code preventing the package from working properly (or if this is how it's supposed to work.)

require('dotenv').config({ path: './config/.env' })
const express = require('express')
const session = require('express-session')
const mysql = require('mysql2')
const Sequelize = require('sequelize')
const path = require('path')
const SequelizeStore = require('connect-session-sequelize')(session.Store)

const app = express()
const PORT = 9999

app.use(express.json())

app.use(express.static(path.join(__dirname, 'public')))

// Allowing connection to database in Workbench
const db = new Sequelize('somedatabase', 'root', process.env.PASSWORD, {
    host: 'localhost',
    dialect: 'mysql'
})

db.authenticate()
    .then(() => {
        console.log('Connected...')
    }).catch(error => {
        console.log('Failed to connect...', error)
    })


// Setting up session
app.use(session({
    secret: 'shhh',
    store: new SequelizeStore({
        db: db
    }),
    resave: false,
    saveUninitialized: true,
    cookie: {
        maxAge: 1000000
    }
}))




// Sample model
const User = db.define('user', {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    username: Sequelize.STRING,
    password: Sequelize.STRING
})


// Sample request
app.post('/api/create', async (req, res) => {
    const newUser = {
        username: john,
        password: verysecurepassword
    }

    await User.create(newUser)
})



app.listen(PORT, () => {
    console.log(`Listening on localhost:${PORT}...`)
})

CodePudding user response:

In this code, you are using several packages: express-session, which manages the session itself but delegates how the session is saved to connect-session-sequelize. So the problem is that connect-session-sequelize is trying to save session data in the database, but it cannot because there is no table for sessions. As written in the documentation of this package (https://www.npmjs.com/package/connect-session-sequelize):

If you want SequelizeStore to create/sync the database table for you, you can call sync() against an instance of SequelizeStore along with options if needed.

So try creating the store, attaching it to the session manager, and then initializing it (I did not test this code):

// Setting up session
var myStore = new SequelizeStore({
  db: db
});
app.use(
  session({
    secret: "shhh",
    store: myStore,
    resave: false,
    saveUninitialized: true,
    cookie: {
        maxAge: 1000000
    }
  })
);

myStore.sync();
  • Related