2017-07-06 165 views
1

我想取消訂閱我的Firebase數據庫中某個位置的訂閱。我正在使用Ionic 3和AngularFire2。如何取消訂閱AngularFire數據庫觀察/訂閱?

但是,我不知道什麼樣的數據類型,使我的訂閱,因爲截至目前這是一個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) 

,因爲它仍試圖讀的東西,現在不能訪問,因爲我」沒有認證。

我的火力地堡規則:

{ 
    "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>;進入爲了既捕獲數據,又能夠退訂?

回答

1

你的情況,你可以使用first操作

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

}); 
0

您是否想取消訂閱?

mySubscription : any; 


mySubscription = this.db.object('/parents/' + auth.uid).subscribe(profileResult => { 
    this.profileResult = profileResult; 
    console.log(profileResult); 

}); 


mySubscription.unsubscribe();