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:
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. 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.
ssn.comport="LebronJames";
=> comport is declared and initialised simultaneously
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.