node.js - Mongoose 和 connect-mongo

标签 node.js mongodb session mongoose session-cookies

我正在使用 mongoose 来管理数据之间的关系,并且我正在尝试使用 connect-mongo 将特定 session 存储在数据库中。

看起来我们需要两次连接到数据库,一次使用 mongoose,另一次使用 connect-mongo。

我正在使用以下代码来初始化 mongoose 的连接

await mongoose.connect(this._connectionUrl, this._connectionOptions);

每次都初始化一个新商店(不确定我在代码初始化方面是否正确)。

app.use(session({
            // secret: config.sessionSecretKey,
            secret: "secretkey",
            resave: true,
            saveUninitialized: true,
            cookie: { maxAge: 19 * 60000 }, // store for 19 minutes
            store: MongoStore.create({
                mongoUrl: this._connectionUrl,
                mongoOptions: this._connectionOptions // See below for details
            })
        }))

有什么方法可以将连接从 mongoose 传递到 mongo-connect Store?

最佳答案

我也在寻找解决方案,只需在 connect-mongo 的“迁移指南”上阅读此内容

For the options, you should make the following changes:

Change url to mongoUrl Change collection to collectionName if you are using it Keep clientPromise if you are using it mongooseConnection has been removed. Please update your application code to use either mongoUrl, client or clientPromise To reuse an existing mongoose connection retreive the mongoDb driver from you mongoose connection using Connection.prototype.getClient() and pass it to the store in the client-option. Remove fallbackMemory option and if you are using it,

还有这个例子https://github.com/jdesboeufs/connect-mongo/blob/master/example/mongoose.js

关于node.js - Mongoose 和 connect-mongo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66731669/

相关文章:

mysql - 在 NodeJS 中 Sequelize : Inner JOIN implementation Failure

mongodb - 无法通过 Mongodb shell 连接到 Mongodb 集群

html - asp session 超时无法正常工作

javascript - 如何从 Meteor.js 中的数组获取数组

mongodb - 如何使用多个键有效地执行 "distinct"?

php - 如何使用 DbSession 在 Yii2 中创建用户 session 管理系统

session - 我的 tmux 状态栏消失了

node.js - NodeJS Sequelize 中真的需要迁移吗?

node.js - 使用 Apache 和反向代理将 domain.com/api 路由到特定端口

node.js - 如何使用 Apollo 和 React Native 在每个 graphql 请求之前实现自动 jwt token 刷新?