Observable/BehaviorSubject 的 Firebase Promise 错误

Firebase Promise Error with Observable/BehaviorSubject

Angular Firebase:我有一个将值设置为 photon2 的函数。我需要从我的 getPhoton() 函数中获取这个值并进入我的 Observable BehaviorSubject isLoginSubject?我得到一个错误:

错误错误:未捕获(承诺):类型错误:无法读取未定义的属性'isLoginSubject'

这是我的 auth-service.ts 代码 如果有任何帮助,我将不胜感激!

  import { Injectable } from '@angular/core';
  import { Router } from '@angular/router';
  import { AngularFireAuth } from 'angularfire2/auth';
  import { AngularFireList, AngularFireDatabase, AngularFireObject, >AngularFireAction } from 'angularfire2/database';
  import * as firebase from 'firebase/app';
  import { Observable } from 'rxjs/Observable';
  import { ProgramService } from '../views/shared/program.service';
  import { BehaviorSubject } from 'rxjs/BehaviorSubject';

  @Injectable()
  export class AuthService {
    _photon: string;
    isLoginSubject = new BehaviorSubject<string>(this._photon);
    public user: Observable<firebase.User>;
    itemRef: AngularFireObject<any>;
    public userDetails: firebase.User = null;
    public LOGGEDIN: boolean = null;

    ownersRef = firebase.database().ref().child('owners');

    constructor(private _firebaseAuth: AngularFireAuth, private router: >Router, private programService: ProgramService,
                private db: AngularFireDatabase) {

      this.user = _firebaseAuth.authState;
      this.user.subscribe(
        (user) => {
          if (user) {
            this.getPhoton(user); // I would like to use this function with >an Observable/BehaviorSubject.
            this.userDetails = user;
            this.getController(this.userDetails.uid); // I don't want to use >the localStorage approach... but it works!
            this.LOGGEDIN = true;
          } else {
            this.userDetails = null;
          }
        }
      );
    }

  getPhoton(user) {
  // retrieves "controller" name from the logged in firebase user. How can I >push that to an Observable/BehaviorSubject
    if (user) {
      this.ownersRef.orderByChild('userId').equalTo(user.uid).once('value')
      .then((snap) => {
        snap.forEach(function(data) {
        const photon2 = data.val().controller;  // QUESTION: How do I make >this an Observable-BehaviorSubject?
        console.log('getPhoton controller: ' + photon2);
        this.isLoginSubject.next(photon2);  // Error: Uncaught (in promise): >TypeError: Cannot set property '_controller' of undefined
        });
      });
    }
  }

我想通了,想 post 为其他掌握异步概念的初学者提供解决方案!我不得不将 1 行 snap.forEach(function (data) { 更改为 snap.forEach(data => {,现在我的错误消失了,我可以 .next() photon2 的值到我的 BehaviorSubject 中。这是我的 auth-service.ts 文件中的完整 getPhoton() 函数,在其下方,我将展示如何从其他组件订阅主题

  getPhoton(user) {
  // retrieves "controller" name from the logged in firebase user that is "next'ed" to an Observable/BehaviorSubject
    if (user) {
      this.ownersRef.orderByChild('userId').equalTo(user.uid).once('value')
      .then((snap) => {
        snap.forEach(data => {
        const photon2 = data.val().controller;
        this.isLoginSubject.next(photon2);
        });
      });
    }
  }

现在我可以从任何组件调用:

ngOnInit() {

  this.authService.isLoginSubject.subscribe((controller) => {
    console.log('holy grail: ' + controller);
  });
}