I want to save the session value "image location" into the database before I have destroyed the session in logout route
The solution adopted by me is :
app.get('/logout',function(req,res){
Person.update({ username: req.session.user_name }, { $set: {lastimage: req.session.userimage[req.session.img_idx]}}, function(error,update)
{
if(update){
req.session.destroy(function() {
res.end();
});
if(error){
console.log(error);
res.end();
}
}
});
});
But when I am using this get location route the task is accomplished ie the value in person db is updated but in return there is some weird error shown
TypeError: Cannot read property 'undefined' of undefined
at options.key (/opt/expressjs/app.js:381:94)
at callbacks (/opt/expressjs/node_modules/express/lib/router/index.js:164:37)
at param (/opt/expressjs/node_modules/express/lib/router/index.js:138:11)
at pass (/opt/expressjs/node_modules/express/lib/router/index.js:145:5)
at Router._dispatch (/opt/expressjs/node_modules/express/lib/router/index.js:173:5)
at Object.router (/opt/expressjs/node_modules/express/lib/router/index.js:33:10)
at next (/opt/expressjs/node_modules/express/node_modules/connect/lib/proto.js:193:15)
at resume (/opt/expressjs/node_modules/express/node_modules/connect/lib/middleware/static.js:65:7)
at SendStream.error (/opt/expressjs/node_modules/express/node_modules/connect/lib/middleware/static.js:80:37)
at SendStream.EventEmitter.emit (events.js:95:17)
Why is it so. I am using ExpressJs sessions and mongodb
PS: The line /opt/expressjs/app.js:381:94 is
Person.update({ username: req.session.user_name }, { $set: {lastimage: req.session.userimage[req.session.img_idx]}}, function(error,update)
Have you tried saving
'req.session.userimage[req.session.img_idx]' in some var before use ?
for example:
var uName = req.session.user_name,
uImage = req.session.userimage[req.session.img_idx];
Person.update({ username: uName }, { $set: {lastimage: uImage }}, function(error,update) { ...
BTW, you may be calling res.end() twice, which will make things weird.
EDIT:
app.get('/logout', function(req,res) {
var uName = req.session.user_name,
uImage = req.session.userimage[req.session.img_idx];
Person.update({ username: uName }, { $set: {lastimage: uImage }}, function(error,update) {
if(error)
console.log(error);
// This actually destroys the session
delete req.session;
res.end();
});
});
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.