【问题标题】:Firebase Promise Error with Observable/BehaviorSubject带有 Observable/BehaviorSubject 的 Firebase Promise 错误
【发布时间】:2018-08-07 03:44:29
【问题描述】:

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

ERROR 错误:未捕获(承诺中):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
        });
      });
    }
  }

【问题讨论】:

    标签: angular firebase angularfire angular2-observables behaviorsubject


    【解决方案1】:

    我想通了,想为其他掌握异步概念的初学者发布解决方案!我不得不将 1 行 snap.forEach(function (data) { 更改为 snap.forEach(data =&gt; {,现在我的错误消失了,我可以将 photon2 的值 .next() 更改为我的 BehaviorSubject。这是来自我的auth-service.tsfile 的完整 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);
      });
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-22
      • 2018-01-21
      • 2023-03-10
      • 1970-01-01
      • 2017-12-05
      • 2017-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多