打字稿方法返回未定义?
typescript method returning undefined?
您好,我有一个 angular 5 应用程序。我有一个 returns undefined 的服务方法。
这就是我想要做的。我有一个名为 cloneFlight 的函数。我正在调用 flightsService.getCampaignsToClone(this.flight) 这是 returning 值未定义。
cloneFlight() {
combineLatest(
this.translateService.get('CONFIRM.CLONE_FLIGHT', { item: this.flight.name}),
this.flightsService.getCampaignsToClone(this.flight)
).subscribe( ([header, campaigns]) => {
this.cloneFlightService.openModal(header,this.flight,campaigns);
});
}
getCampaignsToClone 的代码如下
getCampaignsToClone(flight: Flight){
let campaignStatusesIdArr: string[];
let campaigns: CampaignUnpaginated[] ;
this.campaignService.getStatuses().subscribe(
(data) => {
campaignStatusesIdArr = data.filter( x => x.code === (CampaignStatusCode.IN_PROGRESS ||
CampaignStatusCode.READY)).map( y => y.id);
}
);
let accountId: string = flight.campaign.account.id;
this.campaignService.getUnpaginatedCampaigns(
{
statuses: campaignStatusesIdArr,
accounts: accountId
}
).subscribe(data=>{
console.log(data);
campaigns = data;
});
return Observable.of(campaigns);
}
在 getCampaignsToClone 中,我正在进行 campaignService.getStatuses() returns Observable 的 http 调用。然后过滤掉其中的一些然后我打电话给
getUnpaginatedCampaigns 这是另一个 http 调用。 anyidea 什么是编写此代码的最佳方法,以便该方法不会 return 未定义。我想我可能没有使用 rxjs 运算符。有人可以帮我弄清楚吗?
非常感谢
重写您的 getCampaignsToClone
方法,使其 returns 成为一个 Observable 序列。使用flatMap依次订阅getUnpaginatedCampaigns observable
getCampaignsToClone(flight: Flight): Observable<CampaignUnpaginated[]> {
return this.campaignService.getStatuses().pipe(
map(data => data.filter( x => x.code === (CampaignStatusCode.IN_PROGRESS || CampaignStatusCode.READY)).map(x => x.id)),
flatMap(ids => this.campaignService.getUnpaginatedCampaigns({
statuses: ids,
accounts: flight.campaign.account.id,
}))
);
}
您好,我有一个 angular 5 应用程序。我有一个 returns undefined 的服务方法。 这就是我想要做的。我有一个名为 cloneFlight 的函数。我正在调用 flightsService.getCampaignsToClone(this.flight) 这是 returning 值未定义。
cloneFlight() {
combineLatest(
this.translateService.get('CONFIRM.CLONE_FLIGHT', { item: this.flight.name}),
this.flightsService.getCampaignsToClone(this.flight)
).subscribe( ([header, campaigns]) => {
this.cloneFlightService.openModal(header,this.flight,campaigns);
});
}
getCampaignsToClone 的代码如下
getCampaignsToClone(flight: Flight){
let campaignStatusesIdArr: string[];
let campaigns: CampaignUnpaginated[] ;
this.campaignService.getStatuses().subscribe(
(data) => {
campaignStatusesIdArr = data.filter( x => x.code === (CampaignStatusCode.IN_PROGRESS ||
CampaignStatusCode.READY)).map( y => y.id);
}
);
let accountId: string = flight.campaign.account.id;
this.campaignService.getUnpaginatedCampaigns(
{
statuses: campaignStatusesIdArr,
accounts: accountId
}
).subscribe(data=>{
console.log(data);
campaigns = data;
});
return Observable.of(campaigns);
}
在 getCampaignsToClone 中,我正在进行 campaignService.getStatuses() returns Observable 的 http 调用。然后过滤掉其中的一些然后我打电话给 getUnpaginatedCampaigns 这是另一个 http 调用。 anyidea 什么是编写此代码的最佳方法,以便该方法不会 return 未定义。我想我可能没有使用 rxjs 运算符。有人可以帮我弄清楚吗?
非常感谢
重写您的 getCampaignsToClone
方法,使其 returns 成为一个 Observable 序列。使用flatMap依次订阅getUnpaginatedCampaigns observable
getCampaignsToClone(flight: Flight): Observable<CampaignUnpaginated[]> {
return this.campaignService.getStatuses().pipe(
map(data => data.filter( x => x.code === (CampaignStatusCode.IN_PROGRESS || CampaignStatusCode.READY)).map(x => x.id)),
flatMap(ids => this.campaignService.getUnpaginatedCampaigns({
statuses: ids,
accounts: flight.campaign.account.id,
}))
);
}