0
private nodes = [];

constructor(private nodeService: NodeService) {}    

    this.nodeService.getNodes('APIUrl')
            .subscribe((data) => {
            this.nodes.push(data);
        });


        console.log(this.nodes)

This is my component where I get the data.

getNodes(url) {
    return this.http.get(url)
    .map((data) => data.json())
}

This is my service where i get my data and map it

However when i console.log(this.nodes) in my component this is my result:

[1: Array[0]]

And it is possible to see the structure of my data as shown here

So my problem is when i console.log(this.nodes[0]) in my component, I get undefined. I can't seem to access my data as I need to. Any thoughts?

developer033
  • 24,267
  • 8
  • 82
  • 108
Artulo
  • 7
  • 1

1 Answers1

2

Per Juan's comment, you need to move the console.log to be within the callback.

constructor(private nodeService: NodeService) {}    

    this.nodeService.getNodes('APIUrl')
            .subscribe((data) => {
            this.nodes.push(data);
            console.log(this.nodes)
        });

But really, your code indicates you don't understand the asynchronous nature of that service. Do read and understand the answer he linked to.

Moby Disk
  • 3,761
  • 1
  • 19
  • 38
  • I probably don't. I will take a step back to read up on things more. Thanks! – Artulo Feb 21 '17 at 07:39
  • Oh yea how do you mean the code indicated i don'nt understand? Do you mean due to the fact that I am not utilizing the callback?? – Artulo Feb 21 '17 at 16:43