5.4: Retrieving Users from the Database
Adding Our API's Base Url to Environment Config
Currently, we only have one service, our AuthService
. It's storing our base API URL as a hard-coded string.
As we add other services, it will be easier to have this in our configuration. If we need to make changes (for example, deployment) it will be easier to change it in one location.
Open up the environment.ts
file inside of the environments folder and update it to include the apiUrl
:
Now, we can update our AuthService
class to use this endpoint instead of the one hard-coded:
Update the login()
and register()
methods to access the appropriate endpoint (by adding /auth/
):
Adding a User Service
In the root of your SPA
application, run the following command to generate a new UserService
:
Update the newly generated service to the following:
Make sure that the UserService
is imported and in the providers array in your app.module.ts
file (CLI doesn't automatically add it for some reason):
Display Users in the MemberListComponent
Now, let's display our Users
in our MemberListComponent
.
Open up member-list.component.ts
and add this code:
Now, let's use this in our template to display them.
Add the following to member-list.component.html
.
We'll improve this shortly, but for now let's test to make sure our service is set up correctly and getting data from our backend.
When you login as a user, you should see an ugly list of the names of our users.
Last updated