Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot set property 'userId' of undefined #57

Open
dreeverl20 opened this issue May 6, 2022 · 3 comments
Open

TypeError: Cannot set property 'userId' of undefined #57

dreeverl20 opened this issue May 6, 2022 · 3 comments

Comments

@dreeverl20
Copy link

Hi Ben,

Thank you so much for this tutorial.

If any one can help me I would really appreciate it

I have been stuck on this part for over two days. Any time I add in line 141 I get the error below in my sandbox . I have also added in a screen shot from my types file. I also do not see any cookie being set. I f I remove line 141 I do not get an error it logs me in but still does not set a cookie.

Thanks for any help!

Screen Shot 2022-05-06 at 11 15 10 AM

Screen Shot 2022-05-06 at 11 10 47 AM
)

Screen Shot 2022-05-06 at 11 16 19 AM

Screen Shot 2022-05-06 at 11 17 53 AM

@akumaaayush
Copy link

If you hover into the session from request object, you'll get:

This request's Session object. Even though this property isn't marked as optional, it won't exist until you use the express-session middleware Declaration merging can be used to add your own properties.

The default Session object isn't able to add out custom fields like UserId. My take was to do declaration merging by:
First, updating the tsconfig file to use the declaration i.e, adding up

  "typeRoots": [
    "./src/types", // for express.session declaration merging 
    "./node_modules/@types" 
  ]

And adding this in the types.ts:

import "express-session";

declare module 'express-session' {
    export interface Session {
        userId: number;
    }
}
export type myContext = {
    em: EntityManager<IDatabaseDriver<Connection>>;
    req: Request & { session: Session & Partial<SessionData> };
    res: Response;
}

Also, see check the secure property in the cookie property of app.use(session(...)). I had to set it to false to make the cookie work. Hope this helps.

@ryankert01
Copy link

ryankert01 commented Jul 26, 2022

I first solve this problem with @akumaaayush's approach, and then, I found an issue that is all about this problem, so I came to share it.
#13

@dreeverl20
Copy link
Author

@akumaaayush @ryankert01 thank you for your help but I have tried all this plus everything in #13 and I am still getting the same error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants