0

i wanna display a field in array at table .. how can i access SubjectCode in my Json Response using angular 4 ? .. My code give me error [Error trying to diff '[object Object]'. Only arrays and iterables are allowed]

Json Response :

{
  "$id": "1",
  "Council_ID": 88,
  "place": "bla bla",
  "number": "45",
  "type": 1,
  "date": "2017-11-08T00:00:00",
  "time": "04:51",
  "user_Id": 15,
  "subjects": [
     {
        "$id": "2",
        "subjectCode": "45",
        "type": 1,
        "faculty": "medicine",
        "branch": "اسكندرية",
        "gender": true
     }
 ],
  "absence": []
}

meeting.component.html:

<table class="table table-hover table-condensed text-center">
                            <tbody>
                                <tr *ngFor="let subject of subjectsWithID">
                                    <td > {{subject.subjects.subjectCode}} </td>
                                </tr>
                            </tbody>
                        </table>

meeting.component.ts:

this.dataStorageService.getCouncilId(this.meetingID).subscribe(response => {
                    this.subjectsWithID = response.json();
                    console.log(this.subjectsWithID, 'all Json Resopnse Here')
                });
4
  • What does your console log displays ? Commented Nov 8, 2017 at 9:21
  • it displays json response @trichetriche Commented Nov 8, 2017 at 9:22
  • you're using *ngFor over an object not an array. You should use subjectsWithID.subjects on your *ngFor Commented Nov 8, 2017 at 9:24
  • yes , Thanks for helping Commented Nov 8, 2017 at 9:30

2 Answers 2

2

You should use ngFor over subjects, code should be,

<tr *ngFor="let subject of subjectsWithID.subjects">
   <td > {{subject.subjectCode}} </td>
</tr>
Sign up to request clarification or add additional context in comments.

Comments

1

Your subjects property is an array. If you want to display it, then you should do

{{subject.subjects[0].subjectCode}}

2 Comments

Well because you use ngFor on an object, not an array.
Great, rememebr to mark @Sajeetharan's answer as resolved !

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.