Hi,
Searching for a related question was not showing a result, so asking here. I am trying the tutorial for the photo gallery app but when I click the (click)="addPhotoToGallery()" I get this in the console without the photo being added to the grid:
GET http://localhost:8100/undefined 404 (Not Found)
This is on a windows system. Camera opens and I capture a pic, with the green check mark to save it. After the the green check mark, we then see it is added to the array.
I would consider the post above this one (currently marked as the “solution”) to be backwards.
It’s brittle, because it is very easy to end up with the exact same problem in anywhere else that interacts with PhotoService. The problem, IMHO, is the fact that PhotoService.photos is public. Propagating changes to shared data is arguably the entire reason that ReactiveX exists, so I would highly recommend using it here.
Instead of exposing photos, I would do something like this:
class PhotoService {
photos$ = new BehaviorSubject<Photo[]>([]);
peekPhotos(): Photo[] {
return this.photos$.value;
}
watchPhotos(): Observable<Photo[]> {
return this.photos$;
}
addNewToGallery(): void {
let photo = this.existingAddNewToGallery();
this.photos$.next(photo);
}
}
By decoupling the communication of updates from the process of making an update, we smoothly allow consumers other than the one who made the update to receive updates in a timely fashion, and we eliminate a potential source of bugs.