4

I tried to transform data before resolve by such way, but subscriber gets non-transformed data.

@Injectable()
export class OrderService {
    getAll(): Observable< Array<Order> > {
        let url = 'http://fakeapi.com';
        return this.http.get( url )
          .pipe(
            tap( (data: any) => {
                /*
                * MAKE DATA TRANSFORMATIONS HERE
                */
            }),
            catchError( (err: any, caught: Observable<{}>) => {
              console.log('error while GET : ' + url);
              console.warn(err);
              return caught;
            })
          );
    }
}

here is a subscriber:

this._orderService.getAll().subscribe( data => {
  this.orders = data;
  console.log('\nplanning-overview-page : _orderService.getAll : data ', data);
});
1
  • 2
    you can use .map() Commented Nov 17, 2017 at 14:21

2 Answers 2

7

map is the operator for transforming a value in an observable stream, and you have to make sure it returns a value.

map(data => {
    // do your transformations
    return data;
});
Sign up to request clarification or add additional context in comments.

2 Comments

It works, but there is a note: after I added map() the were arror "Property 'map' does not exist on type 'Observable<Object>' ". There is a solution of the error stackoverflow.com/a/41014638/7455192
yea, it needs to be imported like all the other operators, figured you knew since you were using tap already
0

If you want change data, use pipe, map and code as following:

import { map } from 'rxjs/operators';

getAll(): Observable<Order[]>  {
  return this.service.get<Order[]>("https://url")
    .pipe(
      map(data => {
        /*
        * CHANGE DATA HERE
        */
        return data;
      })
    );
}

Comments

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.