【发布时间】:2018-07-31 05:58:14
【问题描述】:
我正在使用 Angular/Firebase 中的 Promises。成功登录后,我会使用登录用户的uid 调用 firebase。我用它来查找在 Firebase 中分配给他们的用户的“控制器名称”。这可以在snap.val() 中返回控制器名称,但我不知道如何让控制器名称输出在我的其他组件的其他位置可用。这里应该是我的 auth.service.ts 代码和相应的控制台输出,以显示它正在提取 snap.val()now 我如何解析它以获取 controllervalue 并使其可观察?在我的整个计划中?
auth.service.ts
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { AngularFireAuth } from 'angularfire2/auth';
import * as firebase from 'firebase/app';
import { Observable } from 'rxjs/Observable';
import { ProgramService } from '../views/shared/program.service';
@Injectable()
export class AuthService {
public user: Observable<firebase.User>;
public userDetails: firebase.User = null;
public LOGGEDIN: boolean = null;
private ownersRef: any;
constructor(private _firebaseAuth: AngularFireAuth, private router: Router, private programService: ProgramService) {
this.ownersRef = firebase.database().ref().child('owners');
this.user = _firebaseAuth.authState;
this.user.subscribe(
(user) => {
if (user) {
this.userDetails = user;
console.log(this.userDetails);
this.getController(this.userDetails.uid);
this.LOGGEDIN = true;
} else {
this.userDetails = null;
}
}
);
}
getController(id) {
return this.ownersRef.orderByChild('userId').equalTo(id).once('value').then((snap) => {
console.log(snap.val());
console.log('just returned getController snap.val');
return snap.val();
});
}
//more code continues here but not needed...
登录后控制台来自 auth.service.ts 的日志数据
Angular is running in the development mode. Call enableProdMode() to enable
the production mode.
Brian Beardmore
{-LG_PkzB4VV0AjdgKgnG: {…}}-LG_PkzB4VV0AjdgKgnG:
controller: "Pellet_Pirate_1"
email: "dbrianbeardmore@gmail.com"
name: "Brian Beardmore"photo: "https://lh6.googleusercontent.com/-YpMyDa8szOw/AAAAAAAAAAI/AAAAAAAAACY/pw-_ei54cjs/photo.jpg"
userId: "IeA2gx1Jq4N3vDVovVXHSbOhlqu2"
just returned getController snap.val
我想存储controller: ''Pellet_Pirate_1'' 的值并使其可用于我的应用程序。此值从不在用户登录时更改。
我将不胜感激。
谢谢, 布赖恩
【问题讨论】:
标签: angular firebase observable angularfire2