【问题标题】:Firestore unsubscribe is not a functionFirestore 取消订阅不是一项功能
【发布时间】:2020-08-31 14:59:16
【问题描述】:

我在理解 unsubscribe() 函数用于分离侦听器时遇到了一些麻烦。 每次用户单击按钮显示表以更改视图选项时,我都想分离侦听器。 其实这是我的代码。 以前,query 被称为 unsubscribe,正如官方 Firestore 文档中所报告的那样(我还在我的项目中的文档中测试了相同的示例代码),并且在控制台日志中始终返回 unsubscribe is not a function

let query;

$(".btnShowTable").on("click", function () { 
   if(typeof query != "undefined") query.unsubscribe();
   createTable(param);
});

function createTable(param) {
   query = db.collection("test1/test2/"+param).orderBy("dataora", "desc").limit(10);
   query.onSnapshot(function (querySnapshot) {
      querySnapshot.docChanges().forEach(function (change) {
   // get datas
   });
 });

}

我应该对我的代码进行哪些更改?

【问题讨论】:

    标签: javascript firebase google-cloud-firestore


    【解决方案1】:

    query 是一个Query 类型的对象,您可以从API 文档中看到它没有unsubscribe() 函数。您需要做的是按照documentation 中的说明进行操作。它说onSnapshot() 返回一个取消订阅函数,以便在取消订阅时调用。

    // subscribe to query results
    const unsubscribe = query.onSnapshot(...)
    
    // later, unsubscribe the listener
    unsubscribe()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-14
      • 2020-04-07
      • 2019-04-09
      • 2012-05-26
      相关资源
      最近更新 更多