首页 > 解决方案 > Firestore 如何清除Persistence()

问题描述

用户注销后,我需要完全清除当前用户数据。因此我使用 clearPersistence() 函数。它需要在 clearPersistence() 之前运行 terminate()。

 this.afs.firestore.terminate().then(() => {
      this.afs.firestore.clearPersistence().then(() => {
      });
});

在上面运行以清除 Firestore 本地缓存后,我收到此错误。

FirebaseError: The client has already been terminated.

任何建议如何重新初始化 Firestore 实例或任何更好的方法来实现?谢谢。

标签: javascriptfirebasegoogle-cloud-firestoreangularfire2

解决方案


这不是clearPersistence(). 我建议仔细阅读API 文档

必须在 firestore 实例未启动时调用(应用程序关闭后或应用程序首次初始化时)。在启动时,这个方法必须在其他方法(除了 settings())之前调用。如果 firestore 实例仍在运行,promise 将被拒绝,错误代码为 failed-precondition。

注意:clearPersistence() 主要用于帮助编写使用 Cloud Firestore 的可靠测试。它使用一种有效的机制来删除现有数据,但不会尝试安全地覆盖或以其他方式使缓存数据不可恢复。对于对用户会话之间的缓存数据泄露敏感的应用程序,我们强烈建议根本不启用持久性。

第二段告诉您,您可能根本不应该启用持久性。

第一段告诉您,在测试期间使用 clearPersistence 只能在 Firestore 应用程序实例首次初始化后(并且在任何查询之前)或应用程序完全关闭后立即完成。


推荐阅读