javascript - 注销后出现缺少或权限不足的错误

标签 javascript firebase ionic-framework firebase-authentication angularfire2

我在项目中使用 Firebase + Ionic。注销时出现我的问题。我订阅了一些集合中的多个 onSnapshot 事件。我希望只要用户注销,所有订阅都会被取消,但事实并非如此,所以每当我注销时,我都会收到来自 Firebase 的几个错误:

Uncaught Error in onSnapshot: Error: Missing or insufficient permissions.

这是我的代码:

我的 Controller

/**
 * Logout button 'click' event handler
 */
onLogoutPressed(){
  this.fbAuthServ.logout().then(() => {
    this.navCtrl.setRoot(LoginPage);
  });
} 

我的服务提供商

// Firebase Modules
import { AngularFireAuth } from 'angularfire2/auth';

constructor(private afAuth: AngularFireAuth, private utils: UtilsProvider){}

...

  /**
    * Firebase Logout the current user
   */
  async logout(){
    this.afAuth.auth.signOut();
  }

请问我应该怎么做才能避免那些Missing or insufficient permissions错误?

提前致谢!

编辑:我如何订阅 onSnapshot 事件

Controller

ionViewDidEnter(){

    this.fbDataServ.getPlacesByUserAllowed().onSnapshot(placeReceived=> {
      this.placesByUserAllowed = placeReceived.docs.map(placeSnapshot => {
        return this.utils.mapPlaceSnapshot(placeSnapshot )
      });
      this._concatPlaces();

      //Dismiss loading whenever we have data available
      this.utils.dismissLoading();
    });

服务提供商

// Firebase
import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore';

constructor(private afs: AngularFirestore, private fbAuthServ: FirebaseAuthServiceProvider, private utils: UtilsProvider) { }

placesCollection: AngularFirestoreCollection<PlaceModel> = this.afs.collection("places");



  /**
   * Gets the collection of places where the user is 'allowed'
   */
  public getPlacesByUserAllowed(){
    return this.placesCollection.ref
    .where('users.' + this.fbAuthServ.getCurrentUser().uid + '.allowed', '==', true);
  }

最佳答案

由于错误消息提到了 onSnapshot,我假设您正在代码中的某处访问 Firebase 数据库或 Cloud Firestore。

您从数据库中读取的数据配置了要求用户经过身份验证的安全规则。因此,当您注销用户时,不再满足该要求,应用程序失去对该数据的访问权限,并且观察者被取消。

为防止出现此错误,请在注销用户之前删除观察者。

更新

按照 Firestore documentation on detaching a listener 中显示的模式移除观察者/监听者。 .首先保留对从 onSnapshot 获得的返回值的引用。

var unsubscribe = this.fbDataServ.getPlacesByUserAllowed().onSnapshot(placeReceived=> {

然后在注销用户之前调用 unsubscribe(),如下所示:

unsubscribe();

关于javascript - 注销后出现缺少或权限不足的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49404498/

相关文章:

javascript - 解释Javascript代码,填充到字符串的左侧或右侧

javascript - 我可以在 ANGULAR2 中使用 ANGULAR1 模块吗?

javascript - 动态标记无法使用新变量

java - Android - 如何将从 Firebase 存储下载的照片保存到手机?

javascript - 以 Angular 返回页面时加载提要不起作用

javascript - Cordova插件如何安装

javascript - 使用 angularJS 和烂番茄进行 API 调用

node.js - 我正在尝试使用 npm 安装 firebase@4.2.0 和 angularfire2@4.0.0-rc.1 但出现以下错误

javascript - 从 Google Cloud Function 检查 Google Speech API 操作

ionic-framework - ionic 3 : Ion-select unselect selected option