Onload of my App I get "TypeError: Cannot read property 'user' of null"


#1

Hi,
I have been working on this app with a restful API, so I get the error
" TypeError: Cannot read property ‘user’ of null " on load of the app.
I checked for solutions online but can’t find any.
Each time i launch the dev server, it brings up that error without even loading the app first.
I checked and there are users registered on the database and I can login well without problems on the web version. I faced this same error before and thought it came from the server. After a few tricks it went away and now it’s persistent. Am really getting frustrated, any help?


#2

Would be nice, if you can show us some code. It is difficult to help without knowing how you load the data. I would assume that you try to read the user object when starting your app and because your rest api has an asynchronous behavior, it has not yet answered with the user object when your app template wants to read it.


#3

I think this might be the problem but how do I go about solving this. This only happens on app start and I added an extra page for it to read before calling the user object but anytime i visit the login page, the same error keeps coming up forcing the app to stop working.


#4

In template use a *ngIf="user"
In your code you can use a common if statement to check if user is already initiated.


#5

Thanks a lot for the response.
I figured out the problem. It originated from my authService, the authService calls the user object every time the app starts and when it can’t find the object it give the above error.

I removed the user object from my authService constructor and it now works fine.
Hope I challenge helps solve some other person’s problem