我遇到了一个棘手的问题,在Nodejs express web应用程序中, Express-session 中间无法在多个sessison上保留引用类型对象,而它适用于原始类型值。
说我有以下代码块: cachedFunc (引用类型)始终在会话中未定义,但 cachedText (基本类型)保存值。
我甚至试图深度复制 cacheFun ,但仍然无法保留
感谢您的帮助!
const _ = require("lodash");
const express = require("express");
const sessionMiddleWare = require("express-session");
const app = express();
app.use(
sessionMiddleWare({
name: "dzbSessionId",
secret: "keyboard cat",
cookie: { maxAge: 66660000 },
resave: false,
saveUninitialized: true
})
);
app.get("/exam", (req, res, next) => {
console.info(req.session.cachedText);/*Primitive Types can be persistent */
if (!req.session.cachedFunc/*always undefined here for referenced value*/) {
try {
req.session.cachedFunc = function(){};
/*even the copied one still can't work*/
//req.session.cachedFunc = _.cloneDeep(function(){});
req.session.cachedText += "freeText";
} catch (e) {}
}
}
答案 0 :(得分:0)
此问题已修复,因为函数不是JSON规范的有效部分,因此JSON.parse& JSON.stringify将处理它。