具有可观察/行为对象的Firebase Promise错误

时间:2018-08-07 03:44:29

标签: angular firebase angularfire angular2-observables behaviorsubject

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

错误错误:未捕获(承诺):TypeError:无法读取未定义的属性'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
        });
      });
    }
  }

1 个答案:

答案 0 :(得分:0)

我想通了,并想将解决方案发布给其他掌握异步概念的初学者!我不得不将1行snap.forEach(function (data) {更改为snap.forEach(data => {,现在我的错误消失了,可以将photon2的值.next()放入我的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);
  });
}