javascript - express-session req.session.touch 不是函数?

标签 javascript node.js express-session

我正在做一个个人项目,最近包括 express-session 和 cookie-session。虽然我在导航到 localhost:3000/时遇到问题,但我收到一条错误消息,告诉我 req.session.touch 不是 express-session 模块中的函数。您可以在下面找到代码片段:

const express = require('express');
const bodyParser = require('body-parser');
const _ = require('lodash');
const passport = require('passport')
const LocalStrategy = require('passport-local')
const cookieParser = require('cookie-parser');
const session = require('express-session')
let cookieSession = require('cookie-session')
const pug = require('pug');
require('./db/mongoose');
const mongoose = require('mongoose');
const Todo = mongoose.model('Todo');
const User = mongoose.model('User');
let app = express();
const ObjectID = require('valid-objectid');

app.use(bodyParser.urlencoded({
  extended: true
}));

app.use(bodyParser.json());

//Use session
app.use(session({ secret: 'secretomitted', cookie: { maxAge: 0 } }))

//Set cookie session
app.use(cookieParser())
app.use(cookieSession({ name: 'session', secret: 'secretomitted', maxAge: 0 }))

//Set views folder and view engine
app.set('views', './views');
app.set('view engine', 'pug');


passport.use(new LocalStrategy(
  function (email, password, done) {
    User.findOne({ 'email': email }, function (err, user) {
      if (err) { return done(err); }
      if (!user) {
        return done(null, false, console.log('Wrong username'));
      }
      if (!user.validatePassword(password)) {
        return done(null, false, console.log('Wrong Password'));
      }
      return done(null, user);
    });
  }
));


//FE Requests

app.get('/', (req, res) => {
  res.send(pug.renderFile('index.pug'))
  console.log('index rendered')
})

这是我得到的错误:

[nodemon] 1.18.9
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node server/server.js`
express-session deprecated undefined resave option; provide resave option server/server.js:24:9
express-session deprecated undefined saveUninitialized option; provide saveUninitialized option server/server.js:24:9
Started on port 3000
/Users/alessandrocamplese/Desktop/Projects/Mongo-tasker/mongo-tasker/node_modules/express-session/index.js:326
        req.session.touch()
                    ^

TypeError: req.session.touch is not a function
    at ServerResponse.end (/Users/alessandrocamplese/Desktop/Projects/Mongo-tasker/mongo-tasker/node_modules/express-session/index.js:326:21)
    at Array.write (/Users/alessandrocamplese/Desktop/Projects/Mongo-tasker/mongo-tasker/node_modules/finalhandler/index.js:297:9)
    at listener (/Users/alessandrocamplese/Desktop/Projects/Mongo-tasker/mongo-tasker/node_modules/on-finished/index.js:169:15)
    at onFinish (/Users/alessandrocamplese/Desktop/Projects/Mongo-tasker/mongo-tasker/node_modules/on-finished/index.js:100:5)
    at callback (/Users/alessandrocamplese/Desktop/Projects/Mongo-tasker/mongo-tasker/node_modules/ee-first/index.js:55:10)
    at IncomingMessage.onevent (/Users/alessandrocamplese/Desktop/Projects/Mongo-tasker/mongo-tasker/node_modules/ee-first/index.js:93:5)
    at IncomingMessage.emit (events.js:197:13)
    at endReadableNT (_stream_readable.js:1129:12)
    at processTicksAndRejections (internal/process/next_tick.js:76:17)
[nodemon] app crashed - waiting for file changes before starting...

有什么解决办法吗?谢谢。

最佳答案

TL;DR: 只使用一个 session 管理,自定义 session 信息扩展 session 对象不要覆盖它。

老问题,但我有同样的问题,所以对于任何其他试图完成同样事情的人:)。我认为问题是来自不同 session 对象的某种冲突:

//Use session
app.use(session({ secret: 'secretomitted', cookie: { maxAge: 0 } }))

//Set cookie session
app.use(cookieParser())
app.use(cookieSession({ name: 'session', secret: 'secretomitted', maxAge: 0 }))

这是使用 express-session 和 cookie-session。这是来自 expressjs.com 的引述:

A user session can be stored in two main ways with cookies: on the server or on the client. [cookie-session] stores the session data on the client within a cookie, while a module like express-session stores only a session identifier on the client within a cookie and stores the session data on the server, typically in a database.

如前所述,express-session只在cookie中存储一个ID,而cookie-session将所有session数据都存储在cookie中。这very short blog post解释了弄乱 session 数据将如何导致您的错误。这两种 session 存储方式不可避免地会造成这样的冲突,因为它们试图将数据存储在不同的地方。

关于javascript - express-session req.session.touch 不是函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56046527/

相关文章:

javascript - 在 js 部分内渲染 html 部分会导致崩溃

node.js - expressjs、nginx 代理、csurf 和安全 cookie 不起作用

node.js - 如何使用 Node.js 获取 Amazon S3 存储桶的总大小?

javascript - 如何在javascript中的2个模式之间拆分字符串

node.js - Express session 不会保存...除非我刷新浏览器?

node.js - NodeJS 请求重置 session

javascript - 如何检测Android浏览器而不是Android应用程序(WebView)?

javascript - 按下预览按钮时 jquery/javascript 发布到新窗口

javascript - jQuery cookie 插件 - 保存并调用 parsetInt() 值

debugging - 如何以编程方式检测nodejs中的 Debug模式?