Reputation: 379
I'm trying to set up a session variable in my NodeJS application that should store the current user.
Unfortunately, I always get the following error :
TypeError: Cannot set property 'user' of undefined
One of my requisite is that I should not use a database to store the sessions, as it is a student project and I must respect the architecture asked.
Here is the function I use to login my user :
this.POSTLogin =function POSTLogin(LoginInformations,res,req){
//prepare request
var args = {
data:{username:LoginInformations.username,password:LoginInformations.password},
headers:{"Content-Type":"application/json"}
};
//check if credentials are valid
client.post(this.restBaseUrl+"users/login",args,function(data,response){
//logs for debug purpose
console.log(data);
console.log(response);
//if there is a user
if(response.statusCode == 200){
var usr = new User();
usr.email = data.email ;
usr.facebookData= data.facebookData;
usr.firstName = data.firstName ;
usr.id = data.id ;
usr.lastName = data.lastName ;
usr.restToken = data.restToken ;
usr.role = data.role ;
usr.sessions = data.sessions ;
var Rest = new RestService();
//this line doesn't work
req.session.user = usr;
//redirect to his personal page with data
Rest.GETPersonalStats(usr,res);
}else{
//is there is no user with those credentials, send it to login page again
res.redirect('/users');
}
});
};
When I'm not trying to save the data into a session, everything works as expected.
At the start of this file, I have :
var session = require('client-sessions');
var express = require('express');
var app = express();
app.use(session({
cookieName: 'session',
secret: 'someRandomString',
duration: 30 * 60 * 1000,
activeDuration: 5 * 60 * 1000
}));
I did add "client-sessions" into my package.json file. I followed information based on this article.
EDIT: Based on @Shoyeb Memon's answer, I changed to use cookie-session. I now have this in my declaration :
var cookieParser = require('cookie-parser');
var cookieSession = require('cookie-session');
var app = express();
app.use(cookieParser('akey'));
app.use(cookieSession({
name:'session',
keys:['akey'],
maxAge: 24 * 60 * 60 * 1000
}));
but the error didn't change at all... I used this to implement the sessions.
EDIT 2: When I changed my code the first time, I wanted to do it without copy-paste, and I followed instructions online. But I did try copy-pasting all, I removed the "store" and it seems to work fine now.
Upvotes: 0
Views: 842
Reputation: 1159
I will give you an example of it from my previous work which might be helpful to you
I have used express-session
const session = require('express-session');
const bodyParser = require('bosy-parser');
//Set Cookie
app.use(session(
{
name: 'myName',
secret: '#@$#!ng',
resave: true,
saveUninitialized: true,
overwrite: true,
unset: 'destroy',
rolling: true,
"cookie": {
maxAge: 1000 * 60 * 15000000
},
store: new (require('express-sessions'))({
storage: 'mongodb',
instance: mongoose, // optional
host: 'localhost', // optional
port: 27017, // optional
db: 'codepost', // optional
collection: 'mysessions', // optional
expire: 1000 * 60 * 15000000
})
}
));
I have set the cookie and stored my sessions in my database
Using session in one of my api:
const express = require('express');
const app = express();
const router = express.Router();
router.post("/validate", function(req,res){
req.session.phoneNumber = req.body.phoneNumber;//storing value into
// session
let userPhoneNumber = req.session.phoneNumber;//assigning to a var
console.log(userPhoneNumber);
}
Use can use the session to store the data which you have got from a text input.
Upvotes: 4