为什么我的Ionic/Firebase订阅被调用了3次



这是我的代码,我不明白为什么我的代码被执行了3次。

advertenciaMantto() {
this.afs.doc('Core/Programador').valueChanges().subscribe(async (data) => {
await this.afs.firestore.doc(`Core/Programador`).get().then(async data => {
if (data.data().manager.prevencion) {
this.presentAlert();
}
});
});
}

你能帮我吗?

编辑

我尝试了.pipe(first(((,它有效,问题是当我对firestore数据进行更改时,它不会执行,因为它已经执行过一次了。

您使用的.subscribe()方法与.get()的行为不同订阅服务器第一次运行时,它总是不返回任何文档,然后再次运行并从本地缓存返回文档,然后再运行一次从远程(firebase(返回文档,再开始侦听文档中的更改

这是我的代码:

async advertenciaMantto() {
await this.afs.firestore.doc(`Core/Programador`).get().then(async doc => {
if (doc.data().manager.prevencion) {
this.presentAlert();
} else {
this.alertaSubs = this.afs.doc('Core/Programador').valueChanges().subscribe(async () => {
await this.afs.firestore.doc(`Core/Programador`).get().then(async doc => {
if (doc.data().manager.prevencion) {
this.alertaSubs.unsubscribe();
this.presentAlert();
}
})
});
}
});
}

最新更新