【发布时间】:2013-08-29 16:01:07
【问题描述】:
我有一个默认的 Express 应用,我的路由 index.js 文件如下所示:
var user = require('../models/user');
exports.index = function(req, res){
res.render('index', { title: 'Express', object: obj });
};
现在,假设在我的 app.js 文件中,client 对象是连接到我的 redis 服务器的 redis 客户端,我希望能够在我的用户模型中使用该客户端对象,例如避免重复登录尝试。我怎样才能在我的用户模型中访问该对象?这是该模型的代码
var mongoose = require('mongoose');
var userSchema = mongoose.Schema({
email: { type: String, required: true, index: { unique: true } },
password: { type: String, required: true },
name: { type: String, required: true }
});
var User = mongoose.model('User', userSchema);
exports.canLogin = function(mIn, pIn, next){
User.findOne({
email: mIn
}, function(err, user){
if(!err){
if(user.password == pIn)
next(true);
else next(false);
} else next(false);
});
};
【问题讨论】:
-
我不关注你的问题以及redis适合的地方。
-
假设在我的 canLogin 函数中我想检查 redis 中的某些内容。如何从那里访问“客户端”对象?
-
您的“客户”在会话中吗?
-
client对象是在哪里创建的?在一个模块中,导出?canLogin只是 JavaScript,所以我不遵循你想要做的事情。这似乎与 MongoDB 没有任何联系。
标签: node.js mongodb mongoose node-redis