2017-11-03 11 views
0

私はまだRxJSを学んでいますが、サービスによって変更されるデータストリームを観測するデータテーブルをセットアップしようとしています。誰かが自分のコードを見て、この観測からの正しい応答?switchMapタイプのエラーで観測可能

search.service.ts

@Injectable() 
export class SearchService { 
    constructor(
     private apiService: ApiService 
    ) { } 

    private searchSource = new BehaviorSubject<object>(this.getInitialCollections()); 

    public currentMessage = this.searchSource.asObservable(); 

    getInitialCollections(): object { 
     return this.apiService.get('/cmt-api/search/merch/collections/_search'); 
    } 

    search(phrase: string) { 
     let results = this.apiService.post('/cmt-api/search/merch/collections/_search', { phrase: phrase }) 
      .subscribe(data => this.changeMessage(data)); 
    } 

    changeMessage(results: object) { 
     this.searchSource.next(results) 
    } 

    readCollectionResult(result: any): Asset[] { 
     let data = JSON.parse(result._body).data; 
     return data.map(asset => { 
      return { 
       id: asset.id, 
       collectionName: asset.collectionName, 
       collectionId: asset.collectionId, 
       objectType: asset.objectType, 
       displayType: asset.displayType, 
       size: asset.size, 
       createDate: asset.createDate, 
       modifiedDate: asset.modifiedDate, 
       publishedDate: asset.publishedDate, 
       parentFolderId: asset.parentFolderId 
      }; 
     }); 
    } 
} 

datatable.component.ts

@Component({ 
    selector: 'mat-datatable', 
    styleUrls: ['./datatable.component.scss'], 
    templateUrl: './datatable.component.html', 
}) 
export class DatatableComponent { 
    displayedColumns = ['id', 'collectionName', 'displayType', 'size']; 
    datatableDatabase: SearchDataSet | null; 
    dataSource: SearchDataSource | null; 

    constructor(
     private searchService: SearchService 
    ) { 
     this.datatableDatabase = new SearchDataSet(searchService); 
     this.dataSource = new SearchDataSource(this.datatableDatabase); 
    } 
} 

export class SearchDataSet { 
    dataChange: BehaviorSubject<Asset[]> = new BehaviorSubject<Asset[]>([]); 
    get data(): object { return this.dataChange.value; } 

    constructor(
     private searchService: SearchService 
    ) { } 

    getCollections() { 
     this.searchService.currentMessage.subscribe(results => { 
      this.dataChange.next(this.searchService.readCollectionResult(results)) 
     }); 
    } 
} 

export class SearchDataSource extends DataSource<Asset> { 
    constructor(private _searchDataset: SearchDataSet) { 
     super(); 
    } 

    /** Connect function called by the table to retrieve one stream containing the data to render. */ 
    connect(): Observable<Asset[]> { 
     return this._searchDataset.dataChange 
      .switchMap(() => { <----- ERROR (see below) 
       return this._searchDataset.getCollections() 
      }); 
    } 

    disconnect() { } 
} 

エラー:

Argument of type '() => void' is not assignable to parameter of type '(value: Asset[], index: number) => ObservableInput<Asset[]>'. 
+1

'this._searchDataset.getCollections()'は何も返しません。観察可能なものを返す必要があります。 –

答えて

1

これを試してみてください。 getCollectionsはObservableを返す必要があります

getCollections() { 
    return this.searchService.currentMessage.map(results => { 
     return this.searchService.readCollectionResult(results) 
    }).map(value=>{ 
     this.dataChange.next(value) 
     return value 
    }); 
} 
関連する問題