简体   繁体   中英

How do I handle errors in a shared observable in typescript?

I have stumbled on a bit of an issue and have trouble finding a person with a similar issue, which probably indicates I am doing something that I shouldn't be doing..

I am doing a http request that looks like this

return this.httpClient.post(`${this.route}/typegroups`, JSON.stringify(typeGroup)).pipe(share());

I am subscribing to the returning observable twice and wants both subscribers to be notified if an error occurs(and if everything went well of course). Right now it's only the first subscriber that gets the error notification

First subscriber:

obs.subscribe((next:any) => {
  //Success Code
},
error => {
  this.notificationService.showError(error.message)
})

The second subscriber

obs.subscribe(next => {
    console.log("EVERYTHING WENT WELL")
  },
  error => {
    console.log("ERROR")
  },
  () => console.log("COMPLETED"));

The first subscriber is getting the error notification and the error method is executed, the other subscriber however is having the next method is executed. Which makes it think everything went ok. Any idea why this is happening? Perhaps there even is a better way of doing this?

Thanks alot in advance, I'm currently racking my brains out tring to find a solution to this..

这实际上在一夜之间以某种方式解决了......我不知道为什么或如何,但现在它起作用了......

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM