So im developing an angular app and i want to acess a list of nodes that contain the latitude and longitude to put in a map. the way i get the object node is by a Service that basically getAll objects from my mongoDB database
retrieveNodes() {
this.nodeService.getAll()
.subscribe(
data => {
this.nodes = data;
console.log(data);
},
error => {
console.log(error);
});
}
The variable this.nodes is declared as nodes:any; in the beggining
And then i have this where i use the information i get to put the coordinates
writeNodes() {
var summit = L.marker([this.nodes[0].latitude,this.nodes[0].longitude], {
icon: L.icon({
iconSize: [25, 41],
iconAnchor: [13, 41],
iconUrl: 'leaflet/marker-icon.png',
shadowUrl: 'leaflet/marker-shadow.png'
})
});
return summit;
}
when i console.log(nodes[0].latitude) inside the subscribe i got the correct number but when i console.log in the second sample i got undefined.
PS: I run retrieveNodes() in onInit() method and thats the way i populate the variable this.nodes. also, i tested to put the retrieveNodes() as a function which returns a list and called in writeNodes() but i cant populate the list. only if im inside the data=>{
Thanks for the help