Issue
Despite looking at documentation, I am unclear how to throw an error within an if conditional with working with an Observable like so:
openCountrySearchDialog() {
const addRef = this.dialog.open(DiscoverCountriesEntriyDialogComponent, {
width: '600px',
id: 'tid-known-countries-dialog-component',
autoFocus: true,
disableClose: true,
data: []
});
addRef.afterClosed().subscribe(countries => {
if (countries.length > 10) {
console.log("Oops, we got more than 10 countries");
}
this.countries = countries;
this.changeDetector.detectChanges();
});
}
I tried the following:
addRef.afterClosed().subscribe(
(countries) => {
this.countries = countries;
this.changeDetector.detectChanges();
},
(error) => {
if (this.countries.length > 10) {
console.log('Oops, we got more than ten countries', error);
}
}
)
I believe I have to use a pipe()
but if so, I am not implementing it correctly:
addRef
.afterClosed()
.pipe(error => {
if (this.countries.length > 10) {
return error;
}
})
.subscribe(countries => {
this.countries = countries;
this.changeDetector.detectChanges();
});
Solution
Simple version of this will be after you subscribe You can check res
if (this.countries.length>10) {
throw throwError(response);
}
Import through error from rxjs and you will be able to catch your error
,
(error) => {
console.log(error)
}
}
Answered By - Muhammad Ahsan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.