This is my first angular project, and I'm still not familiar that well with Observables and RxJS. In my project, at first I want to fetch all notifications with get request. After that, I want to take id of the last notification, so I could send post request to server to mark them all as read. So the code in service looks like this:
getNotifications(limit: number, page: number): any {
return this.http
.get<INotifications>(
`${API_URL}/notifications?direction=desc&limit=${limit}&order_by=created_at&page=${page}`
)
.pipe(
switchMap((response) => {
const id = response.data[0].id;
return this.markNotificationsAsRead(id);
})
);
}
markNotificationsAsRead(id: number) {
return this.http.post(`${API_URL}/notifications/${id}/mark_all_as_read`, {
id,
});
}
I tried with switchMap and mergeMap
operators, but I get
RangeError: Invalid array length
Code in component:
fetchData() {
this.notificationsService.getNotifications(this.limit, this.meta?.next_page || 1).subscribe(
(response) => {
this.notifications = [...this.notifications, ...response.data];
this.meta = response.meta;
this.isLoading = false;
// const mostRecentNotification = response.data[0].id;
// this.markNotificationsAsRead(mostRecentNotification);
},
(error) => {
this.handleErrors(error);
}
);
}
Btw: I can make it work, by deleting this commented section in fetchData function, and just returning get request without piping another operator, but I wanted to give it a try and do it in service. Any ideas why it wont work?
response?return this.markNotificationsAsRead(id)? That will return the value of your post request to thefetchData()method, not the value of your get request.responsethe same in theswitchMap()AND thefetchData()subscribe?