【问题标题】:Firebase : Extract data from two table and orderFirebase:从两个表中提取数据并排序
【发布时间】:2019-08-09 07:03:17
【问题描述】:

我有两个像这样的 firebase 表 user_groups 和组

我想获取登录用户的所有组和每个组的详细信息并显示在列表中。

我有这样的服务:

async getGroups() {
const user = await this.authSP.getUserInfo()
return await this.angularFire
  .list('user_groups/'+user.uid)
  .snapshotChanges()
  .pipe(
    map(groups =>
      groups.map(c => {
        let grps = [];
        this.angularFire.database.ref("groups/"+c.key).once("value", (snapshot) => {
          let value = snapshot.val();
          grps['admin_user_id']=value.admin_user_id;
          grps['name']=value.name;
          grps['photoURL']=value.photoURL
        });

        return {
          '$key': c.payload.key,...c.payload.val(), grps
        }
      })
    )
  )
}

在我的组件中:

async ionViewWillEnter() {
this.allgroups = []
let GroupsOb = await this.groupSP.getGroups()
GroupsOb.subscribe(groups => {

  this.allgroups = []

  groups.forEach((group) => {
    this.allgroups.push(group)
  })

  this.allgroups.sort(function(a, b) {
      //don't work
      return a.charCodeAt(0) - b.charCodeAt(0);
  });
});
}

在我的 ionic 视图中,我有类似的东西

<ion-content>
<ion-list>
    <ion-item *ngFor="let group of allgroups" (click)="goToEditPage(group.$key)" >
        <ion-avatar slot="start">
        <img *ngIf="group.grps.photoURL" [src]="group.grps.photoURL">
        </ion-avatar>
        <ion-label>
        <h2>{{ group.grps.name }}</h2>
        <!--<p>{{ (group.birthdays | async)?.length }} Members</p>-->
        </ion-label>
    </ion-item>
</ion-list>
</ion-content>

我用 this.allgroups.sort 重新排序数据的代码不起作用,因为 a.charCodeAt 未定义。我尝试使用 a.grps['name] 但结果相同。

我认为从 firebase 获取数据的方法不适合我的服务。我需要更改服务吗?

感谢任何帮助

【问题讨论】:

    标签: firebase ionic-framework ionic4


    【解决方案1】:

    我不知道要给你的完整代码,但你应该查看 rxjs 库。

    您已经在使用 with 订阅 observables。

    您必须使用库工具将两个数据源合并在一起:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-21
      • 1970-01-01
      • 2012-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-07
      相关资源
      最近更新 更多