【发布时间】:2019-07-10 16:14:20
【问题描述】:
我订阅了两个相互嵌套的 httprequest。我的目的是让模型数组在第一个请求中充满对象,而不是发出第二个请求并订阅它,这样我就可以从第一个请求中修改对象。 问题是,在第二个请求中,我正在执行许多对象操作,当我将其保存到存储中时,这些操作不存在..
private _patientListPoll$ = interval(this.listRequestInterval).pipe(
startWith(0),
map(() => this.getPatientList().subscribe(model=>{
this.model = model.map(a => Object.assign({}, a));
const linkedIds = this.model.filter(x => x.linkedUserId && x.statusId === 2).map(x => x.linkedUserId);
this.deviceDataService.getLastActivity(linkedIds).subscribe(data=>{
for (const item of data) {
let patient = this.model.find(x => x.linkedUserId === item.userId);
if (patient) {
Object.assign(patient, { lastActivity: item.lastUploadDate });
const diff = Math.abs(new Date().getTime() - new Date(patient.lastActivity).getTime());
const diffDays = Math.ceil(diff / (1000 * 3600 * 24));
if (diffDays <= 7) {
Object.assign(patient, { filterStatus: 4 });
}
let id = patient.id;
let index = this.model.findIndex(item => item.id === id)
this.model.splice(index, 1, patient)
console.log(this.model)
}
}
this.patientDataStore.savePatientData(this.model);
})
}), share()));
任何想法都会很棒..
在 bryan60 的大力帮助下,我明白了
private _patientListPoll$ = timer(0, this.listRequestInterval).pipe(
switchMap(() => this.getPatientList()),
switchMap(model => {
const linkedIds = model.filter(x => x.linkedUserId && x.statusId === 2).map(x => x.linkedUserId);
this.trialService.getPatientTrialStatusList().subscribe(data=>{
if (data) {
for (const item of data.result) {
for (const patient of model) {
if (item.id === patient.id) {
Object.assign(patient, {trialStatusId: item.state});
console.log(patient)
break;
}
}
}
}
})
return this.deviceDataService.getLastActivity(linkedIds).pipe(
map(data => {
for (const item of data) {
let patient = model.find(x => x.linkedUserId === item.userId);
if (patient) {
Object.assign(patient, {lastActivity: item.lastUploadDate});
const diff = Math.abs(new Date().getTime() - new Date(patient.lastActivity).getTime());
const diffDays = Math.ceil(diff / (1000 * 3600 * 24));
if (diffDays <= 7) {
Object.assign(patient, {filterStatus: 4});
}
let id = patient.id;
let index = model.findIndex(item => item.id === id);
model.splice(index, 1, patient);
}
}
return model;
})
);
}),
tap(model => {
this.patientDataStore.savePatientData(model);
this.model = model;
}),
share());
【问题讨论】: