chandermani / angular6byexample

Source control repository for Angular 6 By Example book.
MIT License
41 stars 82 forks source link

endTracking() fails #1

Open fsebbah opened 5 years ago

fsebbah commented 5 years ago

Hi, I have at the point 3.2 When the exercices are finished, in the workout-history-tracker.services, the function endTracking() fails because : this.currentWorkoutLog is null

If I log the finish function : there are a record: finish this.currentWorkoutLog {"startedOn":"2018-07-11T12:23:11.776Z","completed":false,"exercicesDone":12,"lastExercise":"Side Plank"}

After te router [finish] is called but it seems the startExerciseTimeTracking is called again a second times on the window.setInterval and now the this.currentWorkoutLog = null

And it's not possible to get the route /finish. To resolve this problem, I add if(this.currentWorkoutLog === null) return false. And it works. Thanks, Franck

chandermani commented 5 years ago

HI Frank, I will check and confirm.

chandermani commented 5 years ago

I believe this happened because you pressed the browser back button and the code is not destroying the setInterval object in ngDestroy. Can you update the ngDestroy function to

ngOnDestroy() {
    if (this.exerciseTrackingInterval) clearInterval(this.exerciseTrackingInterval);
    this.tracker.endTracking(false);
  }

And try

chandermani commented 5 years ago

Added this check to repo too 0f0a1307b02a7668f81496ba8e85bfcd60f2d948.

Bigbhardwaj commented 5 years ago

Even after applying ngDestroy() it still persists :

image

bartduisters commented 5 years ago
endTracking(completed: boolean) {
    if (this.currentWorkoutLog) {
      this.currentWorkoutLog.completed = completed;
      this.currentWorkoutLog.endedOn = new Date();
      this.currentWorkoutLog = null;
    }
    this.workoutTracked = false;
  }

The above code is a workaround so you can move on with the code/book. It seems that the endTracking gets called twice, the second time this.currentWorkoutLog is null -> throws the error.