0

I have issue with Observables. I have no trouble with basic communication e.g.: SERVICE:

getTableRecords(): Observable<RecordModel[]> {
        return this.http
            .get( this.API + 'records' )
            .map( (tableRecords: Response) =>  tableRecords.json().data as RecordModel[] )
            .catch(this.handleError);

    }

COMPONENT:

getTableRecords() {
        this.recordService.getTableRecords()
            .subscribe( tableRecords => { this.tableRecords = tableRecords; }, (err) => { this.error = err; console.log(this.error)})
    }

Example on top works well, but I have issue with one extra step. Before I'll display result, I wannt to filter one value based on parametr with communications between two services:

SERVICE I:

getTableRecords(): Observable<RecordModel[]> {
        return this.http
            .get( this.API + 'records' )
            .map( (res: Response) =>  res.json().data as RecordModel[] )
            .catch(this.handleError);

    }

SERVICE II

getRecordDetail(assetId: string) {
        return this.rootService.getTableRecords()
            .map( (tableRecords:RecordModel[]) => {
                console.log("GET RECORDS TO ANOTHER SERVICE: ", tableRecords, assetId);
                (tableRecords:RecordModel[]) => tableRecords.filter( (tableRecord:RecordModel) => tableRecord.assetId === assetId )
            })
    }

COMPONENT:

tableRecord: RecordModel;

getRecordDetail() {
        this.sub = this.activatedRoute.params.subscribe(
            params => {
                if (params[ 'assetId' ] != undefined) {
                    let assetId:string = params[ 'assetId' ]
                   this.complianceDetailService.getRecordDetail(assetId)
                        .subscribe( result => this.tableRecord = result )
                } else console.log("UNDEFINED ASSETID");
            }
        );
    }

In COMPONENT I have issue in .subscribe( result => this.tableRecord = result ) IDE yells that void is not assignable to type RecordModel.

Please explain I can make this sequence: First: Get data from one service, Two: Manipulate data in second service, Three: return data to component.

All of this I want to make with Observables, because same sequence I am able to make with Promises.

Best Regards Uland!

1 Answer 1

1
getRecordDetail(assetId: string) {
        return this.rootService.getTableRecords()
            .map( (tableRecords:RecordModel[]) => {
                console.log("GET RECORDS TO ANOTHER SERVICE: ", tableRecords, assetId);
                return  tableRecords.filter( (tableRecord:RecordModel) => tableRecord.assetId === assetId )
            })
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for answer, but unfortunately If I make it that way, in IDE I got err: [ts] The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly. Type argument candidate 'RecordModel[]' is not a valid type argument because it is not a supertype of candidate 'RecordModel'. Property 'length' is missing in type 'RecordModel'. this: this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.