0

connect-mongo 不适用于共享 mongo 集群。我们在我们的应用程序中添加了“connect-mongo”:“^0.8.2”模块,添加了以下配置详细信息。

"mongoStore": {
        "url" : "user:password@server1:27400/zeta-sessions,server2:27400/zeta-sessions,server3:27400/zeta-sessions",
        "name" : "web-app",
        "secret": "secret",
        "cookie": {
            "path": "/",
            "httpOnly": true,
            "maxAge": 31556952000 // 1 year
        },
"options" : {
                        "server": { "poolsize": 5 }
                        ,"replset": {"rs_name": "zeta-po"}

        },
        "resave": true,
        "saveUninitialized": true,
        "store": null    // NB: this will be overlaid in our module
}

还 ,

// ./lib/mongo-session.js
'use strict';

var session = require('express-session'),
    MongoStore = require('connect-mongo')(session);

/** Creates a Mongo-backed session store.
 *
 * @param {Object} [config] Configuration options for express-session and connect-mongo
 * @returns {Object} Returns a session middleware which is backed by Mongo
 */
module.exports = function (config) {
    // add the 'store' property to our session configuration
    config.mongoStore.store = new MongoStore(config.mongoStore);

    // create the actual middleware
    return session(config.mongoStore);
};

执行服务时出现以下错误。

Possibly unhandled Error: Required MongoStore option `db` missing or is not a string.
    at buildUrlFromOptions (/app/home/appjs/zeta-demo/node_modules/connect-mongo/lib/connect-mongo.js:163:15)
    at new MongoStore (/app/home/appjs/zeta-demo/node_modules/connect-mongo/lib/connect-mongo.js:250:7)
    at module.exports (/app/home/appjs/zeta-demo/web/lib/mongo-session.js:14:31)
    at resolveImpl (/app/home/appjs/zeta-demo/node_modules/kraken-js/node_modules/meddleware/index.js:98:20)
    at resolve (/app/home/appjs/zeta-demo/node_modules/kraken-js/node_modules/meddleware/index.js:53:18)
    at register (/app/home/appjs/zeta-demo/

请分享你的想法。

4

1 回答 1

0

也许有点晚了,但我也遇到了类似的问题,我可以通过将我的 IP 地址添加到 IP 访问列表中来解决它,如本期所述。在您的 MongoDB Atlas 用户中,转到 Security -> Network Access 并添加您的 IP 或将其设置为由任何 IP 访问。

于 2021-04-18T12:58:45.710 回答