无法从Firebase获取对象值

时间:2018-05-05 18:40:10

标签: angular firebase firebase-realtime-database angularfire2

在Angular 5 + Firebase(AngularFire)项目中,我有服务Current,我从Firebase RDB获取当前用户数据:

  export class CurrentService {

  public current;

  constructor(
    private authFb: AngularFireAuth,
    private db: AngularFireDatabase
  ) {
    this.isLoggedIn().pipe(
      tap(user => {
        if (user) {
          this.current = this.db.object(`users/${user.uid}`)
          .valueChanges()
        } else {
          this.current = null;
          console.log("no")
        }
      })
    )
    .subscribe()
  }

  isLoggedIn() {
    return this.authFb.authState.pipe(first())
  }
}

在视图中我想要绑定current值:

{{ currService.current?.username }}

但它的回归null。有什么奇怪的,因为当我用{j}格式的字段和值绑定{{ (currService.current | async) | json }}它的返回完整对象时。我做错了什么?

1 个答案:

答案 0 :(得分:1)

您还必须使用async管道:

{{ (currService.current | async)?.username }}