mongodb-js / connect-mongodb-session

Lightweight MongoDB-backed session store for Connect and Express
Apache License 2.0
176 stars 35 forks source link

Throwing an error "Cannot set property '_emitter' of undefined" #83

Closed siddeshwarnavink closed 4 years ago

siddeshwarnavink commented 4 years ago

Hi, I am getting a very odd error from the package. Here is a snippet of my code

const express = require("express");
const mongoose = require("mongoose");
const session = require("express-session");
const MongoDBStore = require("connect-mongodb-session")(session);

// ...

const MONGODB_URI = "mongodb+srv://<...>:<***>@cluster0-tr6jh.mongodb.net/shop";
const app = express();
const store = MongoDBStore({
  uri: MONGODB_URI,
  collection: "sessions",
});

app.use(
  session({
    secret: "my secret!",
    resave: false,
    saveUninitialized: false,
    store,
  })
);

 // ...

mongoose
  .connect(MONGODB_URI)
  .then(() => {
    // ...
    app.listen(3000);
  })
  .catch((err) => console.log(err));

When I run the code. I get the following error:

/home/siddeshwar/dev/my_shop_app /node_modules/connect-mongodb-session/index.js:55
    this._emitter = new EventEmitter();
                  ^

TypeError: Cannot set property '_emitter' of undefined
    at MongoDBStore (/home/siddeshwar/dev/my_shop_app /node_modules/connect-mongodb-session/index.js:55:19)
    at Object.<anonymous> (/home/siddeshwar/dev/my_shop_app /app.js:19:15)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
[nodemon] app crashed - waiting for file changes before starting...

These are the versions used in package.json

"express": "^4.17.1",
"connect-mongodb-session": "^2.3.2",
"mongodb": "^3.5.8",
"connect-mongodb-session": "^2.3.2",
"mongoose": "^5.9.16",

And I am using node version v10.20.1. Please help me to solve this problem.

vkarpov15 commented 4 years ago

You need to do new MongoDBStore({ ... }), not MongoDBStore({ ... })