【问题标题】:AngularFirebase FirebaseListObservable can't get get uidAngular Firebase Firebase List Observable 无法获取 uid
【发布时间】:2018-01-23 19:21:11
【问题描述】:

我有一个想要显示的用户列表。 哪个有效,我可以显示用户名但不能显示密钥。 {{user.uid}} 或 {{user.key}} 不打印任何内容,{{user.username}} 所有名称。 有什么想法吗?

我的结构是这样的:

组件.ts

    allUsers: FirebaseListObservable<any[]>;

  constructor(private afAuth: AngularFireAuth,
    private db: AngularFireDatabase, private router: Router) {

      this.allUsers = db.list('/users');
   }

组件.html

  <ul class="list-group gamer-list">
  <li class="list-group-item agamer" *ngFor="let user of allUsers | async">
    <div class="material-switch">
     <input id="switch{{user.username}}" name="switch{{user.username}}" type="checkbox"/>
     <label for="switch{{user.username}}" class="switch{{user.username}}"></label>
                    </div>
                    {{user.key}} - {{user.username}}
                </li>
        </ul>

【问题讨论】:

  • 你需要使用.snapshotChanges()方法获取id,查看这个answer或者this
  • 谢谢!不幸的是我不能使用 this.allUsers = db.list('/users').snapshotChanges().map(changes => { return changes.map(c => ({ key: c.payload.key, .. .c.payload.val() })); }); -> 不能使用具有 FirebaseListObservable 类型的 snapshotChanges。

标签: angular firebase firebase-realtime-database angularfire


【解决方案1】:

我的问题的解决方案:

我尝试将代码更改为:

 this.userRef = db.list('/users');
      this.allUsers = this.userRef.snapshotChanges().map(changes => {
          return changes.map(c => ({ key: c.payload.key, ...c.payload.val() 
      }));
     });

一开始我有

allUsers: FirebaseListObservable<any[]>;

但 FirebasListObservable 似乎无法使用 snapshotChanges,我必须这样做:

  userRef: AngularFireList<any>;
  allUsers: Observable<any[]>;

但这仍然是不可能的——我的编码程序中的 this.allUsers 出现错误,它说它是 FirebaseListObservable 类型,但事实并非如此。 所以我改变了我的导入: 来自

import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database-deprecated';

收件人:

import { AngularFireDatabase, AngularFireList  } from 'angularfire2/database';

希望这对所有面临同样问题的人有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 2018-02-08
    • 2020-11-30
    • 2017-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多