firebase - 如何取消订阅 AngularFire db 观察/订阅?

标签 firebase ionic2 angularfire angularfire2 ionic3

我想取消订阅 Firebase 数据库中的某个位置。我正在使用 Ionic 3 和 AngularFire2。

但是我不知道要订阅哪种数据类型,因为到目前为止它是 FirebaseObjectObservable<any>我无法取消订阅。

如果不取消订阅,每当我尝试注销时(当我调用 afAuth.auth.signOut() 时),我都会遇到以下错误:

Runtime Error**
permission_denied at /parents/<auth.uid>: Client doesn't have permission to access the desired data.

Stack**
Error: permission_denied at /parents/<auth.uid>: Client doesn't have permission to access the desired data.
    at G (http://localhost:8100/build/main.js:36366:36)
    at Object.G (http://localhost:8100/build/main.js:36370:86)
    at Lg (http://localhost:8100/build/main.js:36342:98)
    at Ag.g.wd (http://localhost:8100/build/main.js:36335:310)
    at og.wd (http://localhost:8100/build/main.js:36325:364)
    at Yf.Xf (http://localhost:8100/build/main.js:36323:281)
    at ag (http://localhost:8100/build/main.js:36307:464)
    at WebSocket.Ia.onmessage (http://localhost:8100/build/main.js:36306:245)
    at WebSocket.o [as __zone_symbol___onmessage] (http://localhost:8100/build/polyfills.js:2:24340)
    at t.invokeTask (http://localhost:8100/build/polyfills.js:3:9967)

因为它仍在尝试读取现在无法访问的内容,因为我尚未通过身份验证。

我的 Firebase 规则:

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

我的home.ts文件:

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { AngularFireDatabase, FirebaseObjectObservable, FirebaseListObservable } from 'angularfire2/database';
import { AngularFireAuthModule, AngularFireAuth } from 'angularfire2/auth';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  //What datatype should I make this?
  profileResult: FirebaseObjectObservable<any>;

  constructor( public navCtrl: NavController,
               private db: AngularFireDatabase,
               private afAuth: AngularFireAuth ) {


  }

  ngOnInit() {
    //Get authentication state
    this.afAuth.authState.subscribe(
        (auth) => {

          //if authenticated, get profile information for db and display using profileResult
          if (auth != null) {
            this.db.object('/parents/' + auth.uid).subscribe(profileResult => {
              this.profileResult = profileResult;
              console.log(profileResult);
            });
          }
      });
  }

  /* What I would like to do, but can't unsubscribe from FirebaseObjectObservable
  ngOnDestroy() {
    this.profileResult.unsubscribe();
  } 
  */

  //calling this results in my error
  signOut() {
    this.afAuth.auth.signOut();
  }

}

我的home.html文件:

<ion-header>
  <ion-navbar>
    <ion-title>
      My Awesome App
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>

{{ profileResult?.firstName }}<br />
{{ profileResult?.lastName }}<br />
{{ profileResult?.youngestChildAge }}<br />
{{ profileResult?.interests }}<br />
{{ profileResult?.facebookUrl }}<br />
<button full ion-button (click)="signOut()">Sign Out</button>

</ion-content>

我可以改变我的profileResult: FirebaseObjectObservable<any>;什么?为了既能捕获数据,又能够退订?

最佳答案

在您的情况下,您可以使用first运算符

    import 'rxjs/add/operator/first';
    this.db.object('/parents/' + auth.uid).first().subscribe(profileResult => {
    this.profileResult = profileResult;
    console.log(profileResult);

});

关于firebase - 如何取消订阅 AngularFire db 观察/订阅?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44952703/

相关文章:

angular - 在 IONIC v2 中从同一页面更改数据后如何使用新数据重新加载子页面

javascript - 如何使用 AngularJS 和 AngularFire 存储对 "active element"的引用?

angularjs - Firebase 简单登录的 FACTORY : get current user. id(电子邮件/密码)

firebase - 对管理员和普通用户使用相同的 Auth 方法时发生用户冲突 | Firebase 身份验证

android - 找不到与 com.google.android.gms :play-services-base:[15. 0.1,16.0.0 匹配的任何版本)

android - 如何从 Android Studio 获取仪器 APK?

firebase - 如何使用 Firebase 实现 LinkedIn 身份验证?

ios - Ionic 2 RC3 iOS 8 自定义字体字母间距问题

security - 如何限制Firebase数据修改?

android - Android 上的 Firebase 高背景数据使用率