I have an ngrx store that contains multiple states. I'm trying to construct one object (Rent object) by gathering data from each state: Here is my code:
ngOnInit() {
//Fetch the rent and its properties data
const rentID = +this.route.snapshot.params['id'];
let rentState$ = this.store.select('rentState');
let clientState$ = this.store.select('clientState');
let vehiculeState$ = this.store.select('vehiculesState');
let paymentState$ = of(null);//TODO
let importRent$ = forkJoin([rentState$, vehiculeState$, clientState$, paymentState$])
.pipe(mergeMap(([rentData, vehiculeData, clientData, paymentData]: [any, any, any, any]) => {
let rent = new Rent();
rent = rentData.rentList.filter(rent => rent.id === rentID)[0];
rent.vehicule = vehiculeData.vehicules.filter(vehicule => vehicule.id === this.rent.vehicule.id)[0];
rent.client = clientData.clientList.filter(client => client.id === this.rent.client.id)[0];
rent.companion = clientData.companionList.filter(companion => companion.id === this.rent.companion.id)[0];
//TODO: retrieve payments
return of(rent);
}))
importRent$.subscribe(data => console.log('hello'));
}
But I don't get any message 'hello' in my console. For some reason the code the subscription does not happen.
I already have these imports in my code:
import { Observable } from 'rxjs/Observable';
import { forkJoin } from 'rxjs/observable/forkJoin';
import { of } from 'rxjs/observable/of';
import { mergeMap } from 'rxjs/operators';
The store already has data in it. Any idea what am I doing wrong?
combineLatest
? And I don't think you need to put theobservables
in an array as a paramter forforkJoin
forkJoin
requires all source Observables to complete. Maybe in your case it would be sufficient to chainthis.store.select('rentState')
withpipe(take(1))
combineLatest
and it worked. Why don't you submit it as an answer I mark it as one. Thank you