我正在使用 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/