Using Data/Action Streams via RxJS, I want to return/pass an error if a user inputs an invalid bay number. My code currently will return a bay object if it matches the number that the user inputs, BUT I can't figure out how to throw an error if the user inputs an invalid bay number that doesn't exist in my list of bays
- In order for me to share data amongst multiple components/pages, I've done most of my stuff in the BayService class:
private baysUrl = 'api/bays';
bays$ = this.http.get<Bay[]>(this.baysUrl)
.pipe(
tap(data => console.log('Bays: ', JSON.stringify(data))),
catchError(this.handleError)
);
/*--------------------------------------------------------------*/
// Grab A Single Bay
private baySelectedSubject = new BehaviorSubject<number>(0);
baySelectedAction$ = this.baySelectedSubject.asObservable();
selectedBay$ = combineLatest([
this.bays$,
this.baySelectedAction$
])
.pipe(
map(([bays, selectedBayNumber]) =>
bays.find(bay => bay.bayCode === selectedBayNumber)
),
);
selectedBayChanged(selectedBayNumber: number): void {
this.baySelectedSubject.next(selectedBayNumber);
}
- I created an Action stream by making a BehaviorSubject. Then I created a method to emit a value onto my Action stream. I then call this method in the
bay.page.ts
where I pass the input parameter and then emit it to my Data stream. - I then combine my data stream and my action stream and then return a single bay object that matches the value from my action Stream.
- ISSUE: SO, I can already emit the value from a users input into my observable and return the bay object based on the number like so:
onSubmit() {
this.bayDoesNotExistError = false;
this.bayService.selectedBayChanged(this.bayForm.get('bayStart').value);
this.navCtrl.navigateForward([`/results/`]);
this.bayForm.reset();
}
, but HOW do I handle an Invalid number? For example, How, when a user presses the submit button after typing in a number, check if it's invalid against my observable, and then return something back to my component to display on the UI? Currently, I client-side check what the user types and then display an error, But I need to actually check if the input value actually exists in my Observable<bay[]> objects and if it doesn't, return an error or something back to my bay.page.ts file to display in my HTML.
Sorry If I didn't explain this very well as I am trying to figure out how to word it.
Here's my BayService: BayService.ts
Here's my Bay.page.ts: Bay.page.ts