0

I am trying to display some JSON data that comes as HTTP web response, but it's not working.

Below is my http request:

nodes: any;

ngOnInit(): void {
    // Make the HTTP request:
    this.http.get('/assets/TEST.json').subscribe(data => {
        // Read the result field from the JSON response.
        this.nodes=data;
    });
}

And this is the template:

<div *ngFor="let node of nodes">
    <p>{{node.description}}</p>
</div>

This is my json data:

{
    "id": 1,
    "name": "root1",
    "link": "/login"
}

This the error I get in console:

Cannot find a differ supporting object '[object Object]' of type 'root1' where root1 is a component different from the one where the request is made.

15
  • did u tried this.nodes = data.json() ? Commented Oct 11, 2017 at 14:15
  • @Med_Ali_Rachid yes , I tried it, but it doesn't work Commented Oct 11, 2017 at 14:17
  • try to log data => console.log(data) , it shows the json ? @eli Commented Oct 11, 2017 at 14:19
  • @Med_Ali_Rachid yes, I ge the json in console Commented Oct 11, 2017 at 14:21
  • what's the nature of the JSON is it an array, an object.. ? can u edit the post and show a screenshot of ur json Commented Oct 11, 2017 at 14:22

2 Answers 2

1

After our investigations in comments the solution :

  • *ngFor shouldn't be used there since his JSON is an object not an array

so the solution is simply :

<p> id : {{nodes.id}}</p>
<p> name : {{nodes.name}}</p>
<p> link : {{nodes.link}}</p>
Sign up to request clarification or add additional context in comments.

Comments

1

First of all, your JSON is an object, not an array. Therefore, you can't use ngFor. As your object doesn't have a description property (but a name), you may display the name (make sure that nodes has been set) using this template:

<p *ngIf="nodes">{{ nodes.name }}</p>

2 Comments

What if I had two or more JSON objects ?
If it had (ie, your file contains an array), then you could use ngFor.

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.