简体   繁体   English

rxjs firstValueFrom() 从不取消订阅 observble

[英]rxjs firstValueFrom() never unsubscribe the observavble

Greatings,问候,

I'm struggling with the firstValueFrom(), lastValueForm() and even Observable.pipe(take(1))...我正在努力处理 firstValueFrom()、lastValueForm() 甚至 Observable.pipe(take(1))...

I'm coding in type script with Angular 14 and RxJs 7.8.0.我正在使用 Angular 14 和 RxJs 7.8.0 编写类型脚本。 My server is a firebase server which provide stored image url via an API with a getDownloadUrl function as an Observable.我的服务器是一个 firebase 服务器,它通过一个 API 提供存储的图像 url 和一个 getDownloadUrl function 作为 Observable。

What I want to do is download my url one time so I can display the image.我想做的是下载我的 url 一次,这样我就可以显示图像了。

Whenever I try one of this method on my Observable it never unsubscribe so the Observable emit value infinitely.每当我在我的 Observable 上尝试其中一种方法时,它永远不会取消订阅,因此 Observable 会无限地发出值。

I tried to write it with async/await syntax and also with the promise syntax but still not able to make it unsubscribe after the first emission.我尝试用 async/await 语法和 promise 语法编写它,但仍然无法在第一次发射后取消订阅。

I can't actually find any reason why it doesn't work... Can some one help me?我实际上找不到任何它不起作用的原因......有人可以帮助我吗?

returnImage(ref: string){
  return new Promise((resolve,reject)=>{
    firstValueFrom(this.fireStorage.ref(ref).getDownloadURL()).then((url)=>{
      console.log(url);
    },(error)=>{
      reject(error);
    });
  });
}

enter image description here在此处输入图像描述

What I want to do is download my url one time so I can display the image.我想做的是下载我的 url 一次,这样我就可以显示图像了。

Whenever I try one of this method on my Observable it never unsubscribe so the Observable emit value infinitely.每当我在我的 Observable 上尝试其中一种方法时,它永远不会取消订阅,因此 Observable 会无限地发出值。

alreeady try to upgrade rxjs. I was using 7.5.0 but upgraded it to 7.8.0.已经尝试升级 rxjs。我使用的是 7.5.0,但已将其升级到 7.8.0。

I tried to write it with async/await syntax and also with the promise syntax我尝试用 async/await 语法和 promise 语法来写它

Thank you very much for your answers it helped me a lot.非常感谢您的回答,对我帮助很大。

1 _ @konrad put me on the way with his questions: I forgot to unsubscribe to my database observable which emit datasnapshot regularly. 1 _ @konrad 让我带着他的问题上路:我忘记取消订阅我的数据库 observable,它会定期发出 datasnapshot。 I display data on my DOM from this observable and use a function to download image at the right time.我从这个 observable 显示我的 DOM 数据,并使用 function 在正确的时间下载图像。 So I think the different syntaxes I tried were right but they were called many time by each datasnapshot emission...所以我认为我尝试的不同语法是正确的,但每次数据快照发射都会调用它们很多次......

2 _ @Shashan Sooriyahetti thanks to you... now I can use Stackblitz and by rewriting my method and testing it out of the whole system I created... I've been able to find my mistake. 2 _ @Shashan Sooriyahetti 谢谢你......现在我可以使用 Stackblitz 并通过重写我的方法并在我创建的整个系统中测试它......我已经能够找到我的错误。

So actually sorry for that guys and thank you very much for your help and reactivity.所以真的很抱歉那些人,非常感谢你们的帮助和反应。 Have nice coding time;)祝你编码愉快;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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