javascript - 如何使用 Node 检查器跟踪 req.session 变量以了解如何从 request.session 中删除 session 对象

标签 javascript node.js express-session node-inspector

//Initializing session 

app.use(session({

  secret: 'keyboard cat',
  resave: true,
  saveUninitialized: true
  //cookie: { secure: true }
}));

我在创建购物车时遇到了一个问题,我在 session 中设置了购物车对象

req.session.cart = [];

//然后

req.session.cart.push({
                    title : p.title,
                    price : p.price,
                    image : '/static/Product_images/'+p._id+'/'+p.image,
                    quantity:quantity,
                    subtotal : p.price

                });

在那之后我尝试安慰它并且我正在获取其中包含 cart 对象的 session 变量但是在那之后我尝试将它设置为 app.locals.cart 以便与 ejs 一起使用,通过尝试从 express 模块的 get 方法中获取 req.session 但 req.session.cart 给出未定义。

app.use(require('connect-flash')());
app.use(function (req, res, next) {
  res.locals.messages = require('express-messages')(req, res);

 // console.log("New cart variable");
  //console.log(req.session);
  //console.log(req.locals);
  //console.log(req.app.locals.cart);

  if(req.session.cart!=="undefined"){
    app.locals.cart=[];
    app.locals.cart = req.session.cart ; 
  }
  next();
});

我无法理解为什么 session 变量会丢失购物车对象

在 StackOverflow 中回答了一些问题后,我发现没有任何东西可以解决我的困境。所以我决定停止使用简单的 console.log,而是决定使用 nodejs 检查器和 我安装我运行

node --inspect app.js

但我在这里遇到的问题是它限制了它对 app.js 本身的访问 添加购物车功能在 routes/cart.js 中

我想使用 nodejs 检查器模块跟踪请求流,并找出每次从 session 变量中删除 cart 变量的原因

所以我的问题分为两部分,第一部分:-为什么源文件夹只显示 app.js 而不是像 this simple tutorial 中的整个项目

enter image description here 我没有获取其中的项目和文件来设置调试点,源代码只有 app.js 作为默认加载的源代码

第二部分是为什么购物车对象无故从 session 中删除

编辑:-

cart.js

我知道第一次通过时它将是未定义的,但在第二次路径中它不会是因为它将由

var productModel =require('../models/products.js');
var categoryModel =require('../models/category.js');

module.exports=(app)=>{

    app.get('/add-to-cart/:id',function(req,res){

        console.log("Cart get started");
                var quantity,subtotal = 0;
                productModel.findOne({'_id':req.params.id},function(err,p){
                        //console.log("product consoled"+p);
                        if(err){
                            return console.log(err);
                        }
            if( typeof req.session.cart == "undefined"){

                //console.log("Session undefined check");

                quantity=1;
                req.session.cart = [];

                req.session.cart.push({
                    title : p.title,
                    price : p.price,
                    image : '/static/Product_images/'+p._id+'/'+p.image,
                    quantity:quantity,
                    subtotal : p.price

                });

                 console.log("#### The request var session inside cart function start");

                 console.log("#### The request var session var end");

                req.app.locals.cart=[];
                req.app.locals.cart.push({
                    title : p.title,
                    price : p.price,
                    image : '/static/Product_images/'+p._id+'/'+p.image,
                    quantity:quantity,
                    subtotal : p.price

                });
                //console.log(req.app.locals);
                //console.log("Session set ");

                //console.log(req.session.cart);

        console.log("Cart got set");
        console.log(req.session);

            }else{

                    var product = req.session.cart;
                    var productFound = false;
                        product.forEach(function(prod){


                            if(prod.title==p.title){
                                prod.quantity+=1;
                                prod.subtotal=prod.quantity*prod.price;
                                productFound=true;

                            }


                    });

                    req.session.cart=product;
                    if(!productFound){
                            quantity=1;
                            req.session.cart.push({
                            title : p.title,
                            price : p.price,
                            image : '/static/Product_images/'+p._id+'/'+p.image,
                            quantity:quantity,
                            subtotal : p.price

                        });
                    }
            }

        });     console.log("req.session.cart");
                console.log(req.session.cart);

                req.flash('success','product added to cart');
                res.redirect('back');
    });
}

编辑 1:- 我在调用中跟踪了 sessionId,发现它与创建新 session 的可能性相同,但从 session 对象中删除购物车对象的原因仍然是个谜

最佳答案

Cookie 只能保存字符串,但您正在尝试将 cookie 初始化为数组,然后将对象插入其中。 cookie 只会执行基本的 "key":"value"

req.session.cart = {
  title : p.title,
  price : p.price,
  image : '/static/Product_images/'+p._id+'/'+p.image,
  quantity:quantity,
  subtotal : p.price
}

如果你想嵌套它,你可能必须使用 JSON.stringify() 来在单个键上序列化它并反序列化它以查看完整的对象。只要您将 cookie 保持在最大尺寸以下即可。


编辑:

因此,虽然我写的关于 cookie 的内容是正确的,但它没有正确解决 express-sessions 实际上存储服务器端数据并且仅将 session ID 存储在 cookie 中的问题。

使用 express-generator 运行 Express 应用程序使用 node --inspect ./bin/www,您可以选择使用 Chrome's native NodeJS inspector .您应该能够从专用的 Node Inspector 查看源代码,并从那里查看您的整个目录。

在没有看到完整的应用程序结构的情况下,很难确定为什么您只会看到 app.js 文件,因为 Node 在检查时会包含所有引用的文件。据推测,您看不到路由的原因是您将应用程序传递给它们,而不是将路由包含在主应用程序中(见下文)。

const indexRouter = require('./routes/index');
const cartRouter = require('./routes/cart');

const app = express();

app.use(session({
secret: 'keyboard cat',
resave: true,
saveUninitialized: true
//cookie: { secure: true }
}))

app.use('/', indexRouter);
app.use('/cart', cartRouter);

如果我使用 http 并且没有注释掉 cookie: { secure: true } ,我可以复制您的购物车不断消失的问题的唯一方法。

关于javascript - 如何使用 Node 检查器跟踪 req.session 变量以了解如何从 request.session 中删除 session 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49467757/

相关文章:

javascript - Python 中的 Pubnub 存在功能 (GAE)

javascript - 如何使用 js 更改位于 Canvas 中的对象的 css 属性?

node.js - 如何在 sendgrid v3 node.js 中向多个收件人发送电子邮件

node.js - 激活 CloudFlare 时重定向循环

javascript - 为什么 express-session(SameSite 属性)在 Chrome 上不起作用?

node.js - Express-session:如何将用户详细信息添加到 Express session Typescript

javascript - Bcrypt.compareSync 总是返回 false

javascript - 如何删除其中没有连字符的任何元素

node.js - 谷歌云 node.js 灵活的环境

javascript - 如何复制 react-table 的状态以使用 page 和 pageSize 的值