Secbone / koa-session2

Middleware for Koa2 to get/set session
MIT License
152 stars 30 forks source link

Class constructor Store cannot be invoked without 'new' #29

Closed raminious closed 5 years ago

raminious commented 7 years ago

I got this error when try to use your package:

/opt/node/app/web/server/util/session-store/index.js:31
    var _this = _possibleConstructorReturn(this, (RedisStore.__proto__ || Object.getPrototypeOf(RedisStore)).call(this));
                                                                                                             ^

TypeError: Class constructor Store cannot be invoked without 'new'
    at new RedisStore (/opt/node/app/web/server/util/session-store/index.js:6:17)
    at Object.<anonymous> (/opt/node/app/web/server/index.js:60:10)
    at Module._compile (module.js:571:32)
    at loader (/opt/node/app/web/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/opt/node/app/web/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/opt/node/app/web/bin/server.js:1:1)
    at Module._compile (module.js:571:32)
    at loader (/opt/node/app/web/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/opt/node/app/web/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Function.Module.runMain (module.js:605:10)
    at Object.<anonymous> (/opt/node/app/web/node_modules/babel-cli/lib/_babel-node.js:154:22)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)

Store.js

import Redis from 'ioredis'
import { Store } from 'koa-session2'

export default class RedisStore extends Store {

  constructor() {
    super()
    this.redis = new Redis()
  }

  async get(sid) {
    const data = await this.redis.get(`SESSION:${sid}`)
    return JSON.parse(data)
  }

  async set(session, { sid =  this.getID(24), maxAge = 1000000 } = {}) {
    try {
      // Use redis set EX to automatically drop expired sessions
      await this.redis.set(`SESSION:${sid}`, JSON.stringify(session), 'EX', maxAge / 1000)
    } catch (e) {}

    return sid
  }

  async destroy(sid) {
    return await this.redis.del(`SESSION:${sid}`)
  }
}

app:

import RedisStore from './util/session-store'

app.use(session({
  key: 'MY_KEY',
  httpOnly: false,
  store: new RedisStore()
}))
uerbin commented 7 years ago

i meet same error, how can solve it??

raminious commented 7 years ago

@uerbin I think it's babel issue, but I don't know how should I deal with that because I need babel-cli to run my app

Secbone commented 7 years ago

@raminious did you use the babel version koa-session2@babel?

uerbin commented 7 years ago

@raminious i use babel-register ^6.24.0

.babelrc { "presets": ["latest"], "plugins": ["transform-object-rest-spread", "transform-class-constructor-call"] }

at first , plugins is ["transform-object-rest-spread"], "transform-class-constructor-call" is added later, but not work

uerbin commented 7 years ago

"devDependencies": { "babel-loader": "^6.4.0", "babel-plugin-transform-class-constructor-call": "^6.22.0", "babel-plugin-transform-object-rest-spread": "^6.23.0", "babel-preset-latest": "^6.24.0", "babel-register": "^6.24.0", "css-loader": "^0.27.3", "extract-text-webpack-plugin": "^2.1.0", "file-loader": "^0.10.1", "koa-webpack": "^0.3.1", "node-sass": "^4.5.0", "nodemon": "^1.11.0", "sass-loader": "^6.0.3", "style-loader": "^0.13.2", "url-loader": "^0.5.8", "webpack": "^2.2.1" }

uerbin commented 7 years ago

maybe i know it

because the module write in new syntax, and not compile it to es5 version and publish it, so when use it online, it can't run, must with babel-register.

use the option can solve it, but cause new problem.

require('babel-register')({
    ignore: false
}); 
Secbone commented 7 years ago

@uerbin you can run npm install --save koa-session2@babel to use the babel version. Here is the doc.

raminious commented 7 years ago

Thanks @Secbone @uerbin I will close this issue

uerbin commented 7 years ago

@raminious thanks.

ilaipi commented 5 years ago

@Secbone hello, did you merge the latest commits to babel branch?

Secbone commented 5 years ago

@ilaipi Koa2 is required node 7.6.0+, so it doesn't need babel anymore.

ilaipi commented 5 years ago

@Secbone my node is 8.14.0 still not work.

Secbone commented 5 years ago

@ilaipi did you use the master version? could you show me some details?

ilaipi commented 5 years ago

@Secbone maybe it's because I use babel-node, because I need import&export, so I think the babel branch is still needed?

Secbone commented 5 years ago

@ilaipi no, you can use the latest version directly, and use import export with babel.

ilaipi commented 5 years ago

@Secbone my babel plugin don't compile node_modules dir only my src dir.

Secbone commented 5 years ago

@ilaipi yes, just use it. It's compatible. 😄

ilaipi commented 5 years ago

@Secbone have u ever tried this? I just upgraded my babel-node to 7.x, but it still don't work.

// babel-node src/app.js
TypeError: Class constructor Store cannot be invoked without 'new'

"koa-session2": "^2.2.8", (rm node_modules and re-install)

Secbone commented 5 years ago

@ilaipi could you try to disable 'babel-plugin-transform-classes to run it? like this:

presets: [
    ["env", { exclude: ["babel-plugin-transform-es2015-classes"] }]
  ]

And I think maybe you don't need babel-node anymore, you can use the latest version of node (11.4 now). Come on, it's almost 2019 now!

ilaipi commented 5 years ago

@Secbone do u use import & export ? if don't use babel-node, how to use them?

@babel/plugin-transform-es2015-classes became @babel/plugin-transform-classes.

// but not work.
The plugins/built-ins '@babel/plugin-transform-classes' passed to the 'exclude' option are not
// the same as babel-plugin-transform-es2015-classes
Secbone commented 5 years ago

@ilaipi Okay, I will test it later, maybe tonight I will publish a new version of babel branch. 😄

ilaipi commented 5 years ago

@Secbone thank you!

ilaipi commented 5 years ago

@ilaipi Okay, I will test it later, maybe tonight I will publish a new version of babel branch. 😄

@Secbone Hello, I have switch my session manager lib to koa-session-minimal and koa-redis. It seems koa-session2 don't compatible with koa-passport.