JavaScript and TypeScript: Why are your injected services becoming null?

I was injecting an auth service as follows: 

However, it was becoming null in my event handlers, which I was using to detect inactivity from the user (i.e. log them out after x amount of minutes had passed with them remaining idle). That is the goal of the ticket. 

I spent a very large amount of time stuck on this bug, like basically the entire time I was dying of the flu last week, and tried various things to get around it. Nothing was working. Through enlisting the help of OpenAI, I thought I had fixed it by just using localStorage to store the login state, but then I realized that I still needed access to the auth service to actually log the user out after the requisite time had passed. 

Here is the solution, given on Stack Overflow.

If you use this syntax: event = () => { /* ... */ }

Then the functions will be bound to the class instance, and your injected services will not be null. This ended up fixing my issue, though I still ended up needing the localStorage code for some reason. 

Comments

Popular posts from this blog

Unleashing my inner Disney Princess ✩₊˚.⋆☾⋆⁺₊✧ at the 2024 Disney Princesses Half Marathon

The 20-something types of Computer Science majors

The Evenstar