我有这个初始化代码;
app.use(cookieParser);
var redis = require('redis');
var redisOptions = {}
redisOptions.client = redis.createClient(<removed>, 'pub-redis-12124.us-east-1-2.3.ec2.garantiadata.com');
app.use(session({
store: new RedisStore(redisOptions),
secret: 'keyboard cat',
resave: true,
saveUninitialized: true
}));
在我的路由中,我有这个;
router.post('/login', function (req, res) {
var users = req.db.get('users');
users.findOne({email: req.body.email}, function (err, user) {
console.log(user);
if (req.body.password == user.password) {
req.session.id = user._id;
console.log("id:" );
res.send('successful login' + req.session.id);
} else {
// Make sure to log failed auth requests
var log = req.db.get('failed_auth');
var fail_info = {};
fail_info.email = req.body.email;
fail_info.ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
fail_info.timestamp = Date.now();
log.insert(fail_info, function(doc) {
res.send('bad creds');
});
}
});
});
但是,当我尝试查看此代码时,我收到此错误;
TypeError: Cannot set property 'id' of undefined
我的Redis服务器是redislabs提供的免费托管服务器之一。我不确定我是否需要进行身份验证,尽管我不认为我这样做。关于前进方向的任何提示?
编辑:
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var bodyParser = require('body-parser')
var cookieParser = require('cookie-parser')();
var session=require('express-session');
var RedisStore = require('connect-redis')(session);
// New Code
var monk = require('monk');
var db = monk('localhost:27017/manpoints');
var routes = require('./routes/index');
var account = require('./routes/account')
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
忽略这些混乱/未使用的东西,我一直在尝试许多不同的东西试图让它发挥作用。