我有一个关于RxJS的问题. 我正在创建一个Web应用程序来管理协会的成员. 我想创建一个按钮来"重置"一个网站的数据库. 具体步骤如下:
- 向所有会员发送邮箱以重新注册
- 删除日期
- 刷新页面
以下是我编写的代码,它可以工作,但我知道有一些地方是错误的.我是RxJS的新手,所以我不太了解所有的原则……
newYear(){
this.amicalisteService.getAmicalistesValides("").subscribe({
// Envoyer un mail à l'ensemble des amicalistes valides
next: amicalistes => {
for(const amicaliste of amicalistes) {
const to = amicaliste.email;
const cc = null;
const subject = "Adhère à l'AEIR";
const body = ""
this.amicalisteService.sendMail(null, to, cc, subject, body).subscribe({
next: response => {
console.log('E-mail envoyé avec succès !', response);
},
error: error => {
console.error('Erreur lors de l\'envoi de l\'e-mail :', error);
}
});
}
},
complete: () => {
// Supprimer amicalistes, photos et cartes amicalistes
this.amicalisteService.getAmicalistes().subscribe(amicalistes => {
for(const amicaliste of amicalistes){
this.amicalisteService.deleteAmicalisteById(amicaliste.id).subscribe();
}
});
this.imageService.getImages("amicaliste").subscribe(images => {
for(const image of images){
this.imageService.deleteImageByName("amicaliste", this.imageService.getImageName(image.toString())).subscribe();
}
});
this.imageService.getImages("pdf").subscribe(pdfs => {
for(const pdf of pdfs){
this.imageService.deleteImageByName("pdf", this.imageService.getImageName(pdf.toString())).subscribe();
}
})
}
//Refresh...
})
}
我听说在SUBSCRIBE()中使用SUBSCRIBE()不是一种好的做法,但我想不出如何以不同的方式实现它. 不过,我希望在此代码中保留几项内容.如果我没有记错的话,实际上,3个Subscribe()是并行运行的.我想要留着它. 否则,我明白使用SwitchMap可以帮助我,但我似乎无法实现它.有人能给我一些建议吗?
非常感谢你!