title: AngularJS NgOnInIt method losing data on the second visit to my component using router-link
body: <p>I have a component called edit-scrim and inside my component i have 2 arrays of type model "Team", i am using some methods from a service i imported into the component to assign values to the array. On my first visit to the page the arrays are being populated just fine but if i go to another page using router.navigate() and comeback to "edit-scrim" using a routerlink it loses the data for 1 of the arrays. All my functionality to assign data to array is inside ngOnInit and i have done a console.log("check") inside this method to see if it is even being called every time i visit the component and <strong>IT IS</strong> being called so i am not sure what is wrong.</p>
  
  <p><strong>If i refresh my page the data comes back, but if i visit it through a routerlink again it does not</strong></p>
  
  <p>These are my arrays in the component.ts file</p>
  
  <pre><code>teams: Team[];
  myTeams: Team[] = [];
  </code></pre>
  
  <p>Attached is the code snippet for my ngOnInIt method, i use a routerlink to visit another page and comeback to the "edit-scrim" and data is gone.</p>
  
  <pre><code> ngOnInit() {
   console.log("check");
   this.myinit();
  
   }
  
   myinit() {
  
  
  
   this.authService.getAuth().subscribe(auth =&gt; {
    if (auth) {
      this.loggedInUser = auth.email;
    } else {
  
    }
   });
  
   this.teamService.getTeams().subscribe(teams =&gt; {
    this.teams = teams;
    console.log(this.teams);
    for (var i = 0; i &lt; this.teams.length; i++) {
      if (this.teams[i].createdBy == this.loggedInUser) {
        this.myTeams.push(this.teams[i]);
      }
    }
  
   });
  
   console.log(this.myTeams);
  
  
   this.id = this.route.snapshot.params['id'];
  
   //Get Team
   this.scrimService.getScrim(this.id).subscribe(scrim =&gt; {
    this.scrim = scrim;
   });
  
  
   }
  </code></pre>
  
  <p><a href="https://ibb.co/jbpx15" rel="nofollow noreferrer">console.log info after visiting the page twice</a> </p>
  
  <p><strong>EDIT</strong>
  When i do this inside my init function, the first time i visit my page it console.logs the right data, if i go to another page and come back to this, it is lost. :/</p>
  
  <pre><code>    this.teamService.getTeams().subscribe(teams =&gt; {
      this.teams = teams;
      console.log(this.teams);
  
     // this.myTeams = this.teams.filter(function (team) { return 
     team.createdBy == this.loggedInUser; });
     this.myTeams = this.teams.filter(team =&gt; team.createdBy == 
     this.loggedInUser)
     console.log(this.myTeams);
  
  
     });
  </code></pre>
  
  <p><strong>EDIT2</strong></p>
  
  <pre><code> this.authService.getAuth().subscribe(auth =&gt; {
        if (auth) {
          this.loggedInUser = auth.email;
          this.teamService.getTeams().subscribe(teams =&gt; {
        this.teams = teams;
        console.log(this.teams);
  
     //this.myTeams = this.teams.filter(function (team) { return team.createdBy == this.loggedInUser; });
      this.myTeams = this.teams.filter(team =&gt; team.createdBy == this.loggedInUser)
      console.log(this.myTeams);
  
  
      });
        } else {
  
        }
      });
  </code></pre>
  
  <p><strong>EDIT3 -> Didn't work this way :/ maybe i messed up syntax?</strong></p>
  
  <pre><code>this.authService.getAuth().subscribe(auth =&gt; {
        if (auth) {
          this.loggedInUser = auth.email;
        }
      },
        error =&gt; { console.log(error) },
        () =&gt; {
          this.teamService.getTeams().subscribe(teams =&gt; {
            this.teams = teams;
            this.myTeams = this.teams.filter(team =&gt; team.createdBy == this.loggedInUser);
            console.log(this.teams);
            console.log(this.myTeams);
          });
        });
  </code></pre>
  
  <p><strong>EDIT 4 - didn't work, it is not even getting to the stage of doing console.log</strong></p>
  
  <pre><code>this.authService.getAuth().subscribe(auth =&gt; {
        if (auth) {
          this.loggedInUser = auth.email;
        }
      },
        error =&gt; { console.log(error) },
        () =&gt; {
          this.teamService.getTeams().subscribe(teams =&gt; {
            this.teams = teams;
            this.myTeams = this.teams.filter(team =&gt; team.createdBy == this.loggedInUser);
            console.log(this.teams);
            console.log(this.myTeams);
          });
        });
  </code></pre>