0

I have this output when I do a console log:

I have this output when I do a console log

What I want to do is access all five elements but I can't do it. If I do this.tempLines[0] it says that is undefined. Same with this.tempLines[0][0] for example. And same with this.tempLines.lines
The only thing that prints is when I do this.tempLines.

This is the way I push elements to tempLines. I did that to store data inside a subscribe:

retrieveLines(){
   this.lineService.getAll().subscribe(result=>{
       this.lines=result;
       for(let data of this.lines) {
          this.tempLines.push(data)
       }
   });
}

And finally I declare my two array vars like this

lines:any=new Array<any>();
public tempLines:any=new Array<any>();

PS: I know there is many redundancy but I do that because I want to store outside the subscribe method the list of lines to use it in other methods and if I just say that this.lines= result when I console log outside subscribe method I have nothing.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • Can you provide the sample response data response? – Selaka Nanayakkara Nov 30 '20 at 14:25
  • Your "redundancy" isn't helping the way you think it does. No matter what, you're going to have to wait until the subscribe function runs before accessing data in `this.lines` or `this.tempLines`. That's asynchronous programming. – Heretic Monkey Nov 30 '20 at 14:26
  • See also [Can't access object property, even though it shows up in a console log](https://stackoverflow.com/q/17546953/215552) – Heretic Monkey Nov 30 '20 at 14:28
  • Heretic Monkey, i understand what u sayin. the thing is that i have many tables. and for example in this case i need to get all Lines and inside all lines do a findByKey() inside another mongo table and after that do another findByKey() in another table. i need three subscribes. and only can do that if all information of last subscribe is stored. if i do the third subscribe inside the main subscribe i loose all data – Nuno Marques Nov 30 '20 at 14:36

0 Answers0