I am new to Observables. I am using a switchMap to loop through an array of Observables from firestore, retriving two streams, combining the outputs of the streams into a new Observable Array.
createSuperPolicy( product:VehicleProducts, vehicle:Vehicle, id:String, policyCertID:String) : Observable<SuperVehiclePolicy>{
var temp = new SuperVehiclePolicy;
temp.policyID = id,
temp.vendor = product.vendor,
temp.title = product.title,
temp.premium = product.premium,
temp.offer = product.offer,
temp.reg = vehicle.registrationNumber ,
temp.benefits = product.Benefits,
temp.policyCertID = policyCertID
return temp as unknown as Observable<SuperVehiclePolicy>
}
populateVehiclePolicyList = this.vehiclePolicyService.getPolicies().pipe(
switchMap((policies:VehiclePolicy[]) => {
var policyList$: Observable<SuperVehiclePolicy>[] = [];
policies.forEach(policy => {
var product$: Observable<VehicleProducts> = this.vehicleProductService.getProductByID(policy.vehicleProductID)
var vehicle$: Observable<Vehicle> = this.vehicleService.getVehicleByID(policy.vehicleID)
var id$ = of(policy.id)
var certID$ = of(policy.policyCertID)
combineLatest(product$, vehicle$, id$,certID$)
.pipe(map(([product, vehicle, pid,certID]) => this.createSuperPolicy(product,vehicle, pid, certID)))
.subscribe((val) => policyList$.push(val));
});
console.log(policyList$)
return forkJoin(policyList$);
})
)
Calling viewing the contents of the new Array of Observables(policyList$) displays the contents of the array.
Now when I subscribe to populateVehiclePolicyList, as below,
this.displayService.populateVehiclePolicyList.subscribe(((policy: SuperVehiclePolicy[]) => {console.log(policy)}))
Console.log(policy) returns neither a value nor an error.
I am confused as to what is happening.
How can I successfully subscribe to populateVehiclePolicyList, so as to consume the output?