【问题标题】:how to unsubscribe nested real time firebase cloud firestore database?如何取消订阅嵌套的实时 Firebase Cloud Firestore 数据库?
【发布时间】:2020-10-01 17:50:21
【问题描述】:

我是网络开发的新手。我正在尝试通过嵌套侦听关系从 Firestore 取消订阅实时侦听器。我试过让unsub = db.collection().onSnapshot(()=>{}); unsub(); 来自 firestore 文档,但它不起作用。

这是一个嵌套的监听器,类似于:

 db.collection('apple_orange').where('orange_id','==',orangeID).onSnapshot((querySnapshot=>{
    querySnapshot.docChanges().forEach(change => {
      if(change.type ==='added'){
          db.collection('apple_mango').where('apple_id','==',change.doc.data().appleID).onSnapshot((querySnapshot=>{
            querySnapshot.docChanges().forEach(change=>{
              if(change.type === 'added'){
                  // do something
              }
            })
          }))
      }
      if(change.type ==='removed'){
        //unsubscribe listener of apple_mango query
        let unsub = db.collection('apple_mango').where('apple_id','==',change.doc.data().appleID).onSnapshot(()=>{})
        unsub()
      }

    });
  }))

甚至有可能做到这一点吗? 请帮忙。

【问题讨论】:

    标签: javascript node.js firebase google-cloud-firestore


    【解决方案1】:

    不要创建新的查询来取消订阅。使用原始查询中的取消订阅功能。

    const unsub = db.collection(...).where(...).onSnapshot(querySnapshot => {
        if (time_to_unsub) {
            unsub()
        }
    })
    

    您可能必须更改取消订阅的范围以匹配您要执行的操作。但无论哪种方式,都不要为了取消订阅而创建新查询。

    【讨论】:

      猜你喜欢
      • 2019-08-07
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 2020-03-29
      • 1970-01-01
      • 2021-04-08
      • 1970-01-01
      • 2021-11-18
      相关资源
      最近更新 更多