0

I am new to Angular 5 and I am currently trying to display data from a database to a view. But I am definitely doing something wrong since the view is just getting [object object]. I would like some help in what I am doing wrong.

Thank you.

ServiceThe service to use

Component using the service enter image description here

The Component's HTML enter image description here

The View

enter image description here

1
  • 4
    user is a object, to display the user object as is change {{user}} to {{user | json}}. To display an property from the object add: {{user._id}} Commented May 10, 2018 at 8:22

3 Answers 3

4

To display the students (not like JSON) , you need to call object.property , in your case user.first_name, user.last_name, user.user_type. Your component HTML :

<h1>CDM Student</h1>
<div *ngFor="let user of users">
   {{user.first_name}}  {{user.last_name}}
</div>code here

Also in your API http://localhost:3000/api/users , you shouldn't return the PASSWORD in the JSON (be very careful with this kind of information)

Sign up to request clarification or add additional context in comments.

Comments

3

your Code is ok . just add angular json pipe. Have you seen https://angular.io/api/common/JsonPipe

The Component's HTML

<h1>CDM Student</h1>
<div *ngFor="let user of users">
   {{user | json}}
</div>code here

maybe it's helpful for you . Thank you

Comments

0

In your case user is an object, 1. if you want to display an object then use JSON pipe.

   {{user | json}}

2. If you want to display sub property of an object then use

{{user.propertyName}} 

for example :

{{user.firstName}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.