node.js - mongo-connect(session) 的 mongo-connect 错误

标签 node.js express session mongo-collection

四处搜索后,我在 code 中找不到任何解决方案或错误。关于这个错误。我有我的 app.js使用 mongo-connect 在我的 Node JS 应用程序中添加文件宣言 :

const MongoStore = require('connect-mongo')(session)
我有这个错误:

TypeError: Class constructor MongoStore cannot be invoked without 'new' at Object. (/Users/souhailmohamed/project/devops/story website/app.js:11:20) at Module._compile (internal/modules/cjs/loader.js:1063:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) at Module.load (internal/modules/cjs/loader.js:928:32) at Function.Module._load (internal/modules/cjs/loader.js:769:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) at internal/main/run_main_module.js:17:47


有我的app.use下面的代码:
app.use(session({
    secret: 'story book',
    resave: false,
    saveUninitialized: false,
    store: new MongoStore({ mongooseConnection: mongoose.connection })
}))
我非常了解
const MongoStore = require('connect-mongo')(session)
但我不明白这个错误。但它来自 traversy media 的 youtube 教程 LinK

最佳答案

我在我的代码中遇到了同样的问题并使用这个解决了它:

const MongoDbStore = require('connect-mongo');
从 require 语句中删除 (session) 并像这样更新 app.use():
// Session Config
app.use(
    session({
        secret: 'story book',
        resave: false,
        saveUninitialized: false,
        store: MongoDbStore.create({
            mongoUrl: YourDatabaseURL
        })
    })
);

关于node.js - mongo-connect(session) 的 mongo-connect 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66654037/

相关文章:

django - "Django-Gevent"还是 "Django-Nodejs-redis"?

javascript - 构建 :Cannot find module OurFirstAppWithFVisualStudio. 模块.scss

javascript - 路由同时服务参数和请求体nodejs

javascript - Node.js Express - 将路由处理为 app.get ('route' ,folder.file)

php - 将cakephp的auth与hybridauth(oauth)相结合

javascript - Reactjs 在循环中重新渲染组件

javascript - 从 Angular 4 向 Express 发送删除请求中的数组

html问题中的php变量

node.js - nodejs Sequelize orm model.validation 不是函数

php - 我可以安全地删除cpanel中的cagefs/var/cpanel/php/sessions/ea-phpxx(xx是版本)中的 session 吗?