簡體   English   中英

Rxjs combinelatest observable value is undefined

[英]Rxjs combinelatest observable value is undefined

我有以下代碼,其中 Obs2 依賴於 Obs1。 但是,當我使用 combineLatest 時,Obs1 的值一直未定義。 我怎樣才能讓它工作? 我基本上只需要 Obs2$ 來過濾掉 Obs1$ 中存在的值。

  this.Obs2$ = this.dataService.getObs2(this.id); 
  this.Obs1$ = this.dataService.getObs1(this.id)
    .pipe(
      map(item => {
        return item.prop1
      }),
      tap( val => console.log('the tapped value: ', val)) // shows undefined
    );

  this.CombinedObs$ = 
  combineLatest(this.Obs1$,this.Obs2$)
  .pipe(
    map(([Obs1,Obs2]) => {
      return Obs2
        .filter(t => !Obs1.includes(t.prop1)) //obs1 keeps coming in as undefined
        .map(item => {
                return {
                  entityId: item.teamId, 
                  entityName: item.teamName, 
                  entityCategory: ''
                }})
    })
  )

我得到了你做錯的問題是在 map pipe,如果返回結果,請告訴我:

this.Obs2$ = this.dataService.getObs2(this.id); 
  this.Obs1$ = this.dataService.getObs1(this.id)
    .pipe(
      map(items => items.map(item => item.prop1)), // Issue was here
      tap( val => console.log('the tapped value: ', val)) // It will not show undefinded now
    );

  this.CombinedObs$ = 
  combineLatest(this.Obs1$,this.Obs2$)
  .pipe(
    map(([Obs1,Obs2]) => {
      return Obs2
        .filter(t => !Obs1.includes(t.prop1)) //obs1 keeps coming in as undefined
        .map(item => {
                return {
                  entityId: item.teamId, 
                  entityName: item.teamName, 
                  entityCategory: ''
                }}) // I'm sure you will get error over here so please review and handle that accordingly 
    })
  )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM