等待在foreach内部观察到几个rxjs,直到执行完另一个]

问题描述 投票:0回答:1
  • 我想循环通过checked_checkboxes值。
  • foreach我想发出POST请求。
  • 然后,当foreach内部的所有POST请求完成时,我想获取刚刚查询的数据。

问题:

  • 我的get函数与我的post函数是分开的,所以post查询没有完成,它执行get,所以它是空的get导致的结果,因为post尚未发布。

我所看到的可能的:

  • 将可观察的内容转换为promise,然后使用异步等待,对此不太满意
  • 可以使用forkJoin运算符执行一些可观察对象,并等待它们完成。

我的API服务返回RxJs观测值,我在Angular 8中,这是两个函数:首先是AddVacation(),它将发布一些数据,然后getAgentsInSfihtDispo()将发布数据。

addVacation() {
    let counter = 0;
    const shift_id = this.selectedShiftForMaincouranteModify;
    const shift_date = this.modifyForm.value.dateDeb.format('YYYY-MM-DD');
    this.api.sendGetRequest("/api/shift_dates/" + shift_date, true, null, null)
        .subscribe((data) => {
            this.agents_dispo_checked.forEach((agent) => {
                const agent_id = agent.id;
                if (data) {
                    this.api.sendPostRequest('/api/shift_dos', true, null,
                        { shift_id: shift_id, shift_date: shift_date, agent_id: agent_id })
                        .subscribe();
                } else {
                    this.api.sendPostRequest("/api/shift_dates", true, null, { date: shift_date })
                        .subscribe((data3) => {
                            if (data3.error === "L'association existe deja dans la base de données") {
                                this.api.sendPostRequest('/api/shift_dos', true, null,
                                    { shift_id: shift_id, shift_date: shift_date, agent_id: agent_id })
                                    .subscribe();
                            } else {
                                this.api.sendPostRequest('/api/shift_dos', true, null,
                                    { shift_id: shift_id, shift_date: data3.date, agent_id: agent_id })
                                    .subscribe();
                            }
                        });
                }
                counter++;
            });
            if (this.agents_dispo_checked.length === counter) {
                this.isOpenSaisieVacation = false;
                this.getAgentsInShiftAndDispo();
            }
        },
    (err) => console.error(err));
}

getAgentsInShiftAndDispo() {
    this.agentsInShift = [];
    this.agents_dispo = [];
    this.agentsInShiftSelectedFormArray.clear();
    this.agentsDispoFormArray.clear();
    if (this.selectedShiftForMaincouranteModify !== 0 &&
        (this.modifyForm.controls.dateDeb.value !== "" || this.modifyForm.controls.dateDeb.value !== null || this.modifyForm.controls.dateDeb.value !== undefined)) {
        const shift_id = this.selectedShiftForMaincouranteModify;
        const goodFormatDate = this.modifyForm.value.dateDeb.format('YYYY-MM-DD');
        this.api.sendGetRequest('/api/shift_dos/byShiftAndDate/' + shift_id + "/" + goodFormatDate, true, null, null)
            .subscribe((data) => {
                if (data) {
                    data.forEach((item) => {
                        this.agentsInShift.push(item.agent);
                    });
                }
            }, (err) => console.error(err),
            () => {
                this.agentsInShift.map((o, i) => {
                    const control = new FormControl(true); // if first item set to true, else false
                    this.agentsInShiftSelectedFormArray.push(control);
                });
                const difference = this.agentsMaintenance.filter((obj) => {
                    return !this.agentsInShift.some((obj2) => {
                        return obj.id === obj2.id;
                    });
                });
                this.agents_dispo = difference;
                this.agents_dispo.map((o, i) => {
                    const control = new FormControl(false); // if first item set to true, else false
                    this.agentsDispoFormArray.push(control);
                });
            });
    }
}

感谢您引导我进入RxJs运营商的广阔世界。

angular promise rxjs observable
1个回答
0
投票

您将必须将所有Observable推入数组,并使用forkJoin。

private observables = [];

addVacation() {
    let counter = 0;
    const shift_id = this.selectedShiftForMaincouranteModify;
    const shift_date = this.modifyForm.value.dateDeb.format('YYYY-MM-DD');
    this.api.sendGetRequest("/api/shift_dates/" + shift_date, true, null, null)
        .subscribe((data) => {
            this.agents_dispo_checked.forEach((agent) => {
                const agent_id = agent.id;
                if (data) {
                    this.observables.push(this.api.sendPostRequest('/api/shift_dos', true, null,
                        { shift_id: shift_id, shift_date: shift_date, agent_id: agent_id }));
                } else {
                    this.observables.push(this.api.sendPostRequest("/api/shift_dates", true, null, { date: shift_date })
                        .subscribe((data3) => {
                            if (data3.error === "L'association existe deja dans la base de données") {
                                this.observables.push(this.api.sendPostRequest('/api/shift_dos', true, null,
                                    { shift_id: shift_id, shift_date: shift_date, agent_id: agent_id }));
                            } else {
                                this.observables.push(this.api.sendPostRequest('/api/shift_dos', true, null,
                                    { shift_id: shift_id, shift_date: data3.date, agent_id: agent_id }));
                            }
                        });
                }
                counter++;
            });
            if (this.agents_dispo_checked.length === counter) {
                this.isOpenSaisieVacation = false;
                forkJoin(this.observables)
                  .subscribe(val => this.getAgentsInShiftAndDispo());
            }
        },
    (err) => console.error(err));
}

getAgentsInShiftAndDispo() {
    this.agentsInShift = [];
    this.agents_dispo = [];
    this.agentsInShiftSelectedFormArray.clear();
    this.agentsDispoFormArray.clear();
    if (this.selectedShiftForMaincouranteModify !== 0 &&
        (this.modifyForm.controls.dateDeb.value !== "" || this.modifyForm.controls.dateDeb.value !== null || this.modifyForm.controls.dateDeb.value !== undefined)) {
        const shift_id = this.selectedShiftForMaincouranteModify;
        const goodFormatDate = this.modifyForm.value.dateDeb.format('YYYY-MM-DD');
        this.api.sendGetRequest('/api/shift_dos/byShiftAndDate/' + shift_id + "/" + goodFormatDate, true, null, null)
            .subscribe((data) => {
                if (data) {
                    data.forEach((item) => {
                        this.agentsInShift.push(item.agent);
                    });
                }
            }, (err) => console.error(err),
            () => {
                this.agentsInShift.map((o, i) => {
                    const control = new FormControl(true); // if first item set to true, else false
                    this.agentsInShiftSelectedFormArray.push(control);
                });
                const difference = this.agentsMaintenance.filter((obj) => {
                    return !this.agentsInShift.some((obj2) => {
                        return obj.id === obj2.id;
                    });
                });
                this.agents_dispo = difference;
                this.agents_dispo.map((o, i) => {
                    const control = new FormControl(false); // if first item set to true, else false
                    this.agentsDispoFormArray.push(control);
                });
            });
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.