英文:
req.session.user - Property 'user' does not exist on type 'Session' error when trying to pass session data using express-session
问题
I am trying to make a simple login form for users to log into a website, where I then save their session data into a session cookie. I am using express-session and in their documentation it gives this as an example of setting it up:
app.post('/login', express.urlencoded({ extended: false }), function (req, res) {
// login logic to validate req.body.user and req.body.pass
// would be implemented here. for this example any combo works
// store user information in session, typically a user id
req.session.user = req.body.user
// save the session before redirection to ensure page
// load does not happen before session is saved
req.session.save(function (err) {
if (err) return next(err)
res.redirect('/')
})
})
but in my code I keep getting an error at the 'req.session.user' part which says: "Property 'user' does not exist on type 'Session & Partial
I follow all the instructions in the documentation and have this at the top of the program as well:
import session from 'express-session';
app.set('trust proxy', 1);
app.use(session({
name: `First_test`,
secret: 'secret_text',
saveUninitialized: true,
resave: true,
cookie: {
secure: false,
maxAge: 960000
}
}));
When debugging I can see that inside the req.session there is indeed no 'user' property.
I have searched google and stackoverflow but haven't been able to find a solution. Most likely I have missed some small step and I am hoping someone here can help educate me.
Thanks for the help in advance.
英文:
I am trying to make a simple login form for users to log into a website, where I then save their session data into a session cookie. I am using express-session and in their documentation it gives this as an example of setting it up:
app.post('/login', express.urlencoded({ extended: false }), function (req, res) {
// login logic to validate req.body.user and req.body.pass
// would be implemented here. for this example any combo works
// store user information in session, typically a user id
req.session.user = req.body.user
// save the session before redirection to ensure page
// load does not happen before session is saved
req.session.save(function (err) {
if (err) return next(err)
res.redirect('/')
})
})
})
but in my code I keep getting an error at the 'req.session.user' part which says: "Property 'user' does not exist on type 'Session & Partial<SessionData>'" even if I am using the exact same code from the express-session documentation.
I follow all the instructions in the documentation and have this at the top of the program as well:
import session from 'express-session';
app.set('trust proxy', 1);
app.use(session({
name: `First_test`,
secret: 'secret_text',
saveUninitialized: true,
resave: true,
cookie: {
secure: false,
maxAge: 960000
}
}));
When debugging I can see that inside the req.session there is indeed no 'user' property.
I have searched google and stackoverflow but haven't been able to find a solution. Most likely I have missed some small step and I am hoping someone here can help educate me.
Thanks for the help in advance.
答案1
得分: 0
From the @types/express-session
package, we can see:
/**
* This interface allows you to declare additional properties on your session object using declaration merging.
*
* @example
* declare module 'express-session' {
* interface SessionData {
* views: number;
* }
* }
*
*/
interface SessionData {
cookie: Cookie;
}
So you should declare the user
properties on your session object like this:
declare module 'express-session' {
interface SessionData {
user: string;
}
}
app.post('/login', express.urlencoded({ extended: false }), function (req, res) {
req.session.user = req.body.user
})
Package version: "express-session": "^1.17.1"
英文:
From the @types/express-session
package, we can see
/**
* This interface allows you to declare additional properties on your session object using [declaration merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html).
*
* @example
* declare module 'express-session' {
* interface SessionData {
* views: number;
* }
* }
*
*/
interface SessionData {
cookie: Cookie;
}
So you should declare the user
properties on your session object like this:
declare module 'express-session' {
interface SessionData {
user: string;
}
}
app.post('/login', express.urlencoded({ extended: false }), function (req, res) {
req.session.user = req.body.user
})
package version: "express-session": "^1.17.1"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论