Question about RXJS & Ionic 4


#1

Hey folks,

just a question I’m stucked at.

Let’s say I got a data service with the follwing method:

import { AppApiService } from ‘./app-api.service’;
import { Observable, from, of } from ‘rxjs’;
import { tap, map } from ‘rxjs/operators’;

@Injectable({
providedIn: ‘root’
})
export class DataService {
dataRange: any = {
start: null,
end: null
};

orderData: Observable<>;

constructor(private api: AppApiService) {
this.init();
}

async init() {
this.api.getOrders(this.dataRange.start, this.dataRange.end)
.subscribe((data) => {
this.orderData = data;
});
}

And in my subscribe method I would like to map all values of “payment_method” to a different value depending on the mapping, e.g.

orderData: Observable<>;
mapping: any = ;

constructor(private api: AppApiService) {
this.mapping[‘stripe’] = “Credit card”;
this.mapping[“paypal_epress”] = “PayPal”;
console.log(this.mapping);
this.init();
}

async init() {
this.api.getOrders(this.dataRange.start, this.dataRange.end)
.pipe(
map(data => {
data.map(ev => {
if (this.mapping[ev.payment_method])
ev.payment_method = this.mapping[ev.payment_method];
})
})
)
.subscribe((data) => {
this.orderData = data;
});
}

Why is this not working?

Thanks,
Oliver


#2

First thing I would do is put an explicit return in front of data.map.