简体   繁体   English

创建变量 session express

[英]Creation variable session express

I'm looking to create a variable that I could use on all the pages of my project in javascript, so I try to use express, the fact is that I can't assimilate a value to this session variable, my code is below:我想创建一个变量,我可以在我的项目的所有页面上使用 javascript,所以我尝试使用 express,事实是我无法同化这个会话变量的值,我的代码如下:

 var express = require('express'); var session = require('express-session'); var app = express(); app.use(session( { secret:'XASDASDA', resave: true, saveUninitialized: true } )); var ssn ; app.get('/',function(req,res){ ssn=req.session; /* * Here we have assign the 'session' to 'ssn'. * Now we can create any number of session variable we want. * Here we do like this. */ // YOUR CODE HERE TO GET COMPORT AND COMMAND ssn.comport; ssn.comport="LebronJames"; }); console.log(ssn.comport);

the result in the console is the following :控制台中的结果如下:

 Typeerror : cannot set property 'comport' of undefined.

ssn.comport; => comport is not defined in this line. => comport 未在此行中定义。 This is causing the error这导致了错误

Either you have to initialise with the some value or null like ssn.comport='' or skip this line as creating comport variable is done in the next line.您必须使用 some 值或 null 进行初始化,如ssn.comport=''跳过这一行,因为在下一行中创建了 comport 变量。

ssn.comport="LebronJames"; => comport is declared and initialised simultaneously => comport 同时声明和初始化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM