firebase 观察Firestore侦听器时仅触发一次更改类型

wko9yo5t  于 2023-03-19  发布在  其他
关注(0)|答案(1)|浏览(108)

有一个弹出窗口是基于用户将一个操作标记为完成而触发的。但是如果用户勾选了许多项并且没有刷新页面,那么所有之前的弹出窗口都会继续弹出,并且每次都必须从其中的每一个弹出窗口中删除。
不知道这是为什么,我有一个退订。

async checkProgress(){
        debugger
      if (this.Origin == 'SGBA') {
       if (this.data?.action==="update" && this.status == 'Complete') {
        //Gets modified document data to pass into progress popout component
        let newData;
        let q = await this.busiAssessmentService.readOverallData2(this.UID);
        const unsubscribe = await onSnapshot(q, (snapshot) => {
       let  changes = snapshot.docChanges()
       console.log(changes.length)
       changes.forEach((change) => { 
            console.log(change.type)
            console.log("change.type")
          if (change.type === "modified") {
              newData = change.doc.data();
              console.log("Modified doc: ", change.doc.data());
               this.openBox(newData, this.inputSource, this.Category)
              return newData;
              }
            });
          },(error) => {
              console.log(error)
           });
           if (newData !== undefined){
            unsubscribe();
           }
      }
     }
    }
yc0p9oo0

yc0p9oo01#

您在错误的位置取消订阅快照,而不是在this.openBox(newData, this.inputSource, this.Category);行之后的末尾取消订阅。
更新代码:

async checkProgress(){
  if (this.Origin == 'SGBA') {
    if (this.data?.action==="update" && this.status == 'Complete') {
      //Gets modified document data to pass into openBox
      let newData;
      let q = await this.busiAssessmentService.readOverallData2(this.UID);
      const unsubscribe = await onSnapshot(q, (snapshot) => {
        let changes = snapshot.docChanges();
        console.log(changes.length);
        changes.forEach((change) => { 
          console.log(change.type);
          console.log("change.type");
          if (change.type === "modified") {
            newData = change.doc.data();
            console.log("Modified doc: ", change.doc.data());
            this.openBox(newData, this.inputSource, this.Category);
            unsubscribe(); // unsubscribe from the listener after the first popout
          }
        });
      }, (error) => {
        console.log(error);
      });
    }
  }
}

相关问题