有这个函数用于提取用户模块,这个函数在响应中提取用户模块的JSON.
getUserModuleInstalled(): Observable<any[]> {
const userId = this.user.id;
return this._moduleService.getUserModule(userId).pipe(
switchMap((response: any) => {
return from(response.user_module || []).pipe(
concatMap((userModule: any) => {
const moduleId = userModule.module_id;
console.log('User module Id response=> '+moduleId);
return this._moduleService.getSubscriptionModule(moduleId).pipe(
map(subModule => subModule.data.subscription_modules),
catchError(error => {
console.error(error);
return of(null);
})
);
}),
toArray(),
catchError(error => {
console.error(error);
return of([]);
})
);
})
);
}
然后a使用这个函数来获取返回json 在ngOninit中使用这个json和这个subscribe方法
this.getUserModuleInstalled().subscribe((data: any[]) => {
if (data) {
const new_plans = JSON.parse(JSON.stringify(data));
console.log('New plans data:', new_plans);
new_plans.forEach((plan: ModulesSub) => {
return this.plansSubject.next(plan);
});
} else {
console.log('No data received');
}
}, (error: any) => {
// handle the error
console.error('Error:', error);
});
this.plansSubject.subscribe((plan: ModulesSub) => {
this.plans = [plan];
console.log('Plans data:', this.plans);
});
//I make a console log for check return this object and is correct
基于该响应,我需要如下使用subscribe,因为这是 * ngFor正确调用我的方式,示例响应应该是什么
this.plans= [
{
"id": "1",
"name": "POS Coffe Shop",
"price": "5",
"type": "1",
"description": "module for sale , managed inventory, ia asistanace",
"frequency": "month"
}, {
"id": "2",
"name": "POS Coffe Shop",
"price": "5",
"type": "1",
"description": "module for sale , managed inventory, ia asistanace",
"frequency": "month"
}
]