node.js - 使用 connect-mongo 和 mongoose 将 session 存储在 mongodb 中

标签 node.js mongodb express mongoose locomotivejs

当我尝试使用 mongodb 配置express.js session 存储时,我遇到了左右错误。我的框架使用 locomotive 并配置了 mongoose。

在我的 02_mongoose.js 初始化程序目录中,我有这个。

module.exports = function() {
    mongoose.connect('mongodb://localhost:27018/basbac');
    mongoose.model('User', UserSchema);
}

我知道我有一个到数据库的连接,因为我可以将我的用户拉到我的 Controller 中。

DeveloperController.show = function() {

var self = this;
var user = mongoose.model('User');

user.find().exec(function(error, users) {

    if(error) {
        console.log(error);
    } else {
        self.res.json({response: { id: self.param('id'), api: self.param('api'), users: users } });    
    }

});

}

http://localhost:3000/developer/test/?api=hhfkgjhukdsfkjhvsduhvudhcsiudvlskejfbk

{
  response: {
  id: "test",
  api: "hhfkgjhukdsfkjhvsduhvudhcsiudvlskejfbk",
  users: [
    {
      _id: "52706695a43c83a739358de5",
      firstname: "cad",
      lastname: "bane",
      address: "duro",
      email: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ae9ebeecae8ebe4efa4e9e5e7" rel="noreferrer noopener nofollow">[email protected]</a>"
    },
    {
      _id: "52706695a43c83a739358de6",
      firstname: "jar jar",
      lastname: "binks",
      address: "naboo",
      email: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0d676c7f676c7f4d6f6463667e236e6260" rel="noreferrer noopener nofollow">[email protected]</a>"
    }
   ]
   }
}

在我的 config/all.js 中,我将其作为 session 配置

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

this.use(express.cookieParser());

this.use(express.session({ 
    secret: 'keyboard cat',
    store: new MongoStore({
        mongoose_connection: mongoose.connection
    }) 
}));

但这会引发错误。

this.db = new mongo.Db(options.mongoose_connection.db.databaseName,
TypeError: Cannot read property 'databaseName' of undefined

我也尝试像 connect-mongo 文档那样做,但我也遇到了错误。 ( https://github.com/kcbanner/connect-mongo ) mongoose_connection 形式为: someMongooseDb.connections[0] 使用现有的 mongoose 连接。 (可选)

this.use(express.session({ 
    secret: 'keyboard cat',
    store: new MongoStore({
        mongoose_connection: mongoose.connections[0]
    }) 
}));

但是我得到了和以前一样的错误。

this.db = new mongo.Db(options.mongoose_connection.db.databaseName,
TypeError: Cannot read property 'databaseName' of undefined

我也尝试按照很多文章所说的去做。这是某人工作配置的示例(Logout in ExpressJS, PassportJS and MongoStore)

this.use(express.session({ 
    secret: 'keyboard cat',
    store: new MongoStore({
        db: mongoose.connection.db
    }) 
}));

但这也会产生错误,而且我知道 db 键实际上是未定义的

throw new Error('Required MongoStore option `db` missing');

将此连接传递到新的 MongoStore 时我做错了什么?当我启动 console.log() Mongoose 对象时,我无法找到有关它正在使用的连接的任何信息。我确实看到了一个基础对象,但它里面没有数据库 key 。我需要将更多选项传递到 Mongoose 配置中吗?

最佳答案

问题在于 Locomotive 的启动顺序。根据the docs :

When a Locomotive application is started, it proceeds through a sequence of steps:

Configure the Environment

In this step, config/environments/all.js is executed followed by the configuration file for the current environment. For instance, when running in development, config/environments/development.js is executed.

Invoke Initializers

After the environment has been configured, initializers are invoked. Initializers are used to configure sub-systems and connect to databases, message queues, and other services utilized by the application.

因此,当您的环境代码被调用时,初始化程序尚未运行,并且 Mongoose 尚未配置。尝试将 Mongoose 设置移动到一个单独的文件(我自己使用 app/db/mongoose ,但这是个人喜好的问题),并在您的环境文件中require

关于node.js - 使用 connect-mongo 和 mongoose 将 session 存储在 mongodb 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19819803/

相关文章:

node.js - (Express js 4 Multer) 在文件上传之前检查表单字段(如果为空)

node.js - 如何在Express和NodeJS中进行Mongoose错误处理。

node.js - Mongoose 连接方法在简单 Node 服务器上失败。 express 、 Mongoose 、路径

javascript - 如何在我自己的应用程序中包含对库的更改?

node.js - Azure Web 聊天测试显示 "There was an error sending this message to your bot: HTTP status code Unauthorized"

node.js - 通过 Mongoose.js 使用 SSL 和 mongos 连接到 Compose.io MongoDB 部署

ruby-on-rails - 如何在 Rails 中混合使用 mongodb 和传统数据库?

node.js - 使用 mongoose 处理 $in

node.js - 计算引擎 : "This site can’ t be reached"

mongodb - 为什么我的 mongodb 索引这么大