【问题标题】:Mongoose and connect-mongoMongoose 和 connect-mongo
【发布时间】:2021-06-18 05:58:40
【问题描述】:

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

看起来我们需要两次连接到数据库,一次使用 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?

【问题讨论】:

    标签: node.js mongodb session mongoose session-cookies


    【解决方案1】:

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

    对于选项,您应该进行以下更改:

    如果您是,请将 url 更改为 mongoUrl 将集合更改为 collectionName 使用它 Keep clientPromise if you are using it mongooseConnection has 被移除。请更新您的应用程序代码以使用 mongoUrl、client 或 clientPromise 重用现有的 mongoose 连接从你的猫鼬连接中检索 mongoDb 驱动程序 使用 Connection.prototype.getClient() 并将其传递给存储在 客户选项。删除 fallbackMemory 选项,如果你正在使用它,

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-20
      • 1970-01-01
      • 2018-12-13
      • 2021-06-13
      • 1970-01-01
      • 1970-01-01
      • 2016-07-24
      相关资源
      最近更新 更多