chill117 / express-mysql-session

A MySQL session store for the express framework in node
MIT License
313 stars 109 forks source link

Cannot create a session store along with mysql2 #144

Closed kalio753 closed 1 year ago

kalio753 commented 1 year ago

I am trying to connect to use the library but I got this error:

Ignoring invalid configuration option passed to Connection: schema. This is currently a warning, but in future 
versions of MySQL2, an error will be thrown if you pass an invalid configuration option to a Connection        
node:internal/errors:490
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "superCtor" argument must be of type function. Received undefined        
    at new NodeError (node:internal/errors:399:5)
    at Object.inherits (node:util:249:11)
    at new module.exports (/mnt/d/NodeJS/RunnerUp/node_modules/express-mysql-session/index.js:42:7)
    at file:///mnt/d/NodeJS/RunnerUp/app.js:49:22
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Node.js v18.16.1

This is my code:

import express from "express"
import cors from "cors"
import dotenv from "dotenv"
dotenv.config()
import swaggerJsdoc from "swagger-jsdoc"
import swaggerUi from "swagger-ui-express"
import session from "express-session"
import MySQLStore from "express-mysql-session"
import mysql from "mysql2/promise.js"

import noteRoute from "./routes/Note.js"

export const app = express()
app.use(cors())
app.use(express.json())
const port = process.env.PORT || 5000

const options = {
    host: process.env.MYSQL_HOST,
    user: process.env.MYSQL_USER,
    password: process.env.MYSQL_PASSWORD,
    database: process.env.MYSQL_DATABASE,
    port: "3306",
    clearExpired: true,
    checkExpirationInterval: 900000, // 15 minutes (in milliseconds)
    expiration: 86400000, // 24 hours (in milliseconds)
    createDatabaseTable: true,
    connectionLimit: 10,
    schema: {
        tableName: "sessions",
        columnNames: {
            session_id: "sid",
            expires: "expires",
            data: "data",
        },
    },
}

const sessionStore = new MySQLStore(options, mysql.createPool(options))

app.use(
    session({
        secret: "your_secret_key",
        resave: false,
        saveUninitialized: false,
        store: sessionStore,
    }),
)
// .....
chill117 commented 1 year ago

Change this:

const sessionStore = new MySQLStore(options, mysql.createPool(options))

To:

const sessionStore = new MySQLStore(options);

And be sure to use a randomly generated value for your session secret:

secret: "your_secret_key"