Node.js MongoStore:无法初始化客户端。不接受mongoose连接对象

Node.js MongoStore:无法初始化客户端。不接受mongoose连接对象,node.js,mongoose,express-session,connect-mongo,Node.js,Mongoose,Express Session,Connect Mongo,我正在尝试使用AWS DocumentDB(AWS品牌的MongoDB)来帮助我存储会话数据。我已经成功地使用mongoose连接到我的db.js文件中的数据库 当我尝试将此mongoose连接作为我的Mongoostore构造函数中的mongooseConnection参数传递给时,出现以下错误: Assertion failed: You must provide either mongoUrl|clientPromise|client in options xxxx/node_module

我正在尝试使用AWS DocumentDB(AWS品牌的MongoDB)来帮助我存储会话数据。我已经成功地使用mongoose连接到我的db.js文件中的数据库

当我尝试将此mongoose连接作为我的Mongoostore构造函数中的mongooseConnection参数传递给时,出现以下错误:

Assertion failed: You must provide either mongoUrl|clientPromise|client in options
xxxx/node_modules/connect-mongo/build/main/lib/MongoStore.js:119
            throw new Error('Cannot init client. Please provide correct options');
                  ^

Error: Cannot init client. Please provide correct options
我的db.js如下所示:

import * as fs from 'fs';
import mongoose from 'mongoose';

var ca = [fs.readFileSync("./certs/rds-combined-ca-bundle.pem")];  // AWS-provided cert

var connectionOptions = {
    useUnifiedTopology: true,
    useNewUrlParser: true,
    ssl: true,
    sslValidate: true,
    checkServerIdentity: false,
    sslCA: ca,
    replicaSet: 'rs0',
    readPreference: 'secondaryPreferred',
    retryWrites: false
};

var connectionString = 'mongodb://' + user + ':' + pwd + '@' + dbServerLocation + '/' + dbName;  // variables defined elsewhere and removed from this post.

mongoose.connect(connectionString, connectionOptions)
    .catch((err) => console.log(err));
}

export default mongoose.connection;
import db from './db/db.js';
import session from 'express-session';
import MongoStore from 'connect-mongo';

db.on('error', () => console.error('MongoDB connection error.'));
db.on('reconnectFailed', () => console.error("Reconnection attempts to DB failed."));
db.on('connected', () => { console.log('Connected to DocumentDB database in AWS') });

import express from 'express';

const sessionStore = new MongoStore({
    mongooseConnection: db,
    collection: 'sessions'
})

var app = express();
app.use(session({
    resave: false,
    secret: 'secret word',
    saveUninitialized: true,
    store: sessionStore,
    cookie: {
        maxAge: 1000 * 60 * 60 * 24
    }
}))
const sessionStore = new MongoStore({
    client: dbConnection.getClient(),
    collectionName: 'sessions'
})
抛出错误的my server.js(main)如下所示:

import * as fs from 'fs';
import mongoose from 'mongoose';

var ca = [fs.readFileSync("./certs/rds-combined-ca-bundle.pem")];  // AWS-provided cert

var connectionOptions = {
    useUnifiedTopology: true,
    useNewUrlParser: true,
    ssl: true,
    sslValidate: true,
    checkServerIdentity: false,
    sslCA: ca,
    replicaSet: 'rs0',
    readPreference: 'secondaryPreferred',
    retryWrites: false
};

var connectionString = 'mongodb://' + user + ':' + pwd + '@' + dbServerLocation + '/' + dbName;  // variables defined elsewhere and removed from this post.

mongoose.connect(connectionString, connectionOptions)
    .catch((err) => console.log(err));
}

export default mongoose.connection;
import db from './db/db.js';
import session from 'express-session';
import MongoStore from 'connect-mongo';

db.on('error', () => console.error('MongoDB connection error.'));
db.on('reconnectFailed', () => console.error("Reconnection attempts to DB failed."));
db.on('connected', () => { console.log('Connected to DocumentDB database in AWS') });

import express from 'express';

const sessionStore = new MongoStore({
    mongooseConnection: db,
    collection: 'sessions'
})

var app = express();
app.use(session({
    resave: false,
    secret: 'secret word',
    saveUninitialized: true,
    store: sessionStore,
    cookie: {
        maxAge: 1000 * 60 * 60 * 24
    }
}))
const sessionStore = new MongoStore({
    client: dbConnection.getClient(),
    collectionName: 'sessions'
})
。。。以及应用程序的其余部分

为了能够将我的mongoose连接对象用作会话存储,我需要更改什么


我在别处看过,但类似的问题表明,我们应该能够发送实际的mongoose连接,而不是重新发送连接字符串并在连接上加倍:

根据此处的文档,Mongoostore构造函数的mongooseConnection参数已更改为“client”:

(我仍然会遇到错误——特别是现在我得到的“con.db”不是来自MongoStore的函数……但由于它与OP有关,答案是改为“client”而不是“mongooseConnection”。)

对于那些在我之后的人-特别是那些认为他们有一个mongo连接并希望将其作为
客户端
参数传递的人。。。。您需要调用
getClient()
函数来帮助实现这一点,如下所示:

import * as fs from 'fs';
import mongoose from 'mongoose';

var ca = [fs.readFileSync("./certs/rds-combined-ca-bundle.pem")];  // AWS-provided cert

var connectionOptions = {
    useUnifiedTopology: true,
    useNewUrlParser: true,
    ssl: true,
    sslValidate: true,
    checkServerIdentity: false,
    sslCA: ca,
    replicaSet: 'rs0',
    readPreference: 'secondaryPreferred',
    retryWrites: false
};

var connectionString = 'mongodb://' + user + ':' + pwd + '@' + dbServerLocation + '/' + dbName;  // variables defined elsewhere and removed from this post.

mongoose.connect(connectionString, connectionOptions)
    .catch((err) => console.log(err));
}

export default mongoose.connection;
import db from './db/db.js';
import session from 'express-session';
import MongoStore from 'connect-mongo';

db.on('error', () => console.error('MongoDB connection error.'));
db.on('reconnectFailed', () => console.error("Reconnection attempts to DB failed."));
db.on('connected', () => { console.log('Connected to DocumentDB database in AWS') });

import express from 'express';

const sessionStore = new MongoStore({
    mongooseConnection: db,
    collection: 'sessions'
})

var app = express();
app.use(session({
    resave: false,
    secret: 'secret word',
    saveUninitialized: true,
    store: sessionStore,
    cookie: {
        maxAge: 1000 * 60 * 60 * 24
    }
}))
const sessionStore = new MongoStore({
    client: dbConnection.getClient(),
    collectionName: 'sessions'
})
在connect mongo的迁移wiki中找到以下内容: