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 IT IS being called so i am not sure what is wrong.
If i refresh my page the data comes back, but if i visit it through a routerlink again it does not
These are my arrays in the component.ts file
teams: Team[];
myTeams: Team[] = [];
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.
ngOnInit() {
console.log("check");
this.myinit();
}
myinit() {
this.authService.getAuth().subscribe(auth => {
if (auth) {
this.loggedInUser = auth.email;
} else {
}
});
this.teamService.getTeams().subscribe(teams => {
this.teams = teams;
console.log(this.teams);
for (var i = 0; i < 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 => {
this.scrim = scrim;
});
}
console.log info after visiting the page twice
EDIT 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. :/
this.teamService.getTeams().subscribe(teams => {
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 => team.createdBy ==
this.loggedInUser)
console.log(this.myTeams);
});
EDIT2
this.authService.getAuth().subscribe(auth => {
if (auth) {
this.loggedInUser = auth.email;
this.teamService.getTeams().subscribe(teams => {
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 => team.createdBy == this.loggedInUser)
console.log(this.myTeams);
});
} else {
}
});
EDIT3 -> Didn't work this way :/ maybe i messed up syntax?
this.authService.getAuth().subscribe(auth => {
if (auth) {
this.loggedInUser = auth.email;
}
},
error => { console.log(error) },
() => {
this.teamService.getTeams().subscribe(teams => {
this.teams = teams;
this.myTeams = this.teams.filter(team => team.createdBy == this.loggedInUser);
console.log(this.teams);
console.log(this.myTeams);
});
});
EDIT 4 - didn't work, it is not even getting to the stage of doing console.log
this.authService.getAuth().subscribe(auth => {
if (auth) {
this.loggedInUser = auth.email;
}
},
error => { console.log(error) },
() => {
this.teamService.getTeams().subscribe(teams => {
this.teams = teams;
this.myTeams = this.teams.filter(team => team.createdBy == this.loggedInUser);
console.log(this.teams);
console.log(this.myTeams);
});
});