使用req.session.userID时出错

时间:2018-04-28 11:47:33

标签: node.js express

我正在使用req.session.userID变量为用户设置sessionID。 我的代码是

在服务器端,

router.post('/signin', bodyParser.json(),function(req, res){
var db = req.db;
var collection = db.get('userCollection');
var name = req.body.name;
var password = req.body.password;
name = name.trim();
password = password.trim();

collection.find({"name":name}, {}, function(e1, username){
    if (e1 === null){

        if (username[0].password == password) {
            req.session.userID = username[0]._id;
            var string2 = "before session" + req.session.userID + "after session" + username[0]._id;
            res.send(string2);
        }
        else {
            res.send("Failure!");
        }
    }
});

在客户端

router.post('/signin', bodyParser.json(),function(req, res){
var db = req.db;
var collection = db.get('userCollection');
var name = req.body.name;
var password = req.body.password;
name = name.trim();
password = password.trim();

collection.find({"name":name}, {}, function(e1, username){
    if (e1 === null){

        if (username[0].password == password) {
            req.session.userID = username[0]._id;
            var string2 = "before session" + req.session.userID + "after session" + username[0]._id;
            res.send(string2);
        }
        else {
            res.send("Failure!");
        }
    }
});

我还在我的app.js中设置了一个秘密令牌,如其他一些答案所建议的那样,但是每当我使用req.session.userID时,它都会给我一个错误。

我删除req.session.userID并发回正常语句的那一刻起作用。任何人都可以告诉我这可能是什么原因。

我的app.js看起来像这样...

var session = require('express-session');
app.use(session({
    secret: 'secret token'
}))

我甚至安装了快递会议。我也应该做别的事吗? 完整的app.js代码,

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var session = require('express-session');


var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/learnproject1');

var logger = require('morgan');

var bodyParser = require('body-parser');
var indexRouter = require('./routes/myroutes.js');
//var userRouter = require('.')

var app = express();



// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());

app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());

app.use(function(req,res,next){
    req.db = db;
    next();
});

app.use('/', indexRouter);
//app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

app.use(session({
    secret: 'secret token'
}))
// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

0 个答案:

没有答案