好的,所以我正在尝试为应用创建排行榜。用户将其“点”存储在Firebase数据库中的用户ID下。
JSON如下:
- users
- hJfEgXkyaKPckchL3kx8rpZ58Ew2
-LV6c8E5rRD4_mQqsb6Q
- name: "Lin Manuel Miranda"
- points: 120
- mlIBrdjT8CfIURQEAhLFPzzUFQg1
-LV7I6d8LeuWFLH5MRKy
- name: "Emily Blunt"
- points: 200
“ mlIBrdjT8CfIURQEAhLFPzzUFQg1”和“ hJfEgXkyaKPckchL3kx8rpZ58Ew2”是相应的user.uid。
如何编写代码,以便以升序返回他们的“要点”列表?
我的.ts文件和.html:
import { Component } from '@angular/core';
// firebase imports
import * as firebase from 'firebase';
import { AngularFireDatabase, AngularFireList } from '@angular/fire/database';
import { User } from 'src/models/login.interface';
import { Observable } from 'rxjs';
import { reference } from '@angular/core/src/render3';
@Component({
selector: 'app-tab1',
templateUrl: 'tab1.page.html',
styleUrls: ['tab1.page.scss']
})
export class Tab1Page {
users: Observable<any>;
constructor(
private db: AngularFireDatabase
) {
this.users = this.db.list('/users/').valueChanges();
const usersRef = firebase.database().ref('/users/');
const ref = usersRef.orderByKey();
ref.once('value').then(function(snap) {
snap.forEach(function (childSnap) {
const pkey = childSnap.key;
console.log(pkey);
});
});
}
}
<ion-content>
<div class="cards" *ngFor="let user of users | async">
<ion-card>
<ion-card-header>
<ion-card-title>{{user.name}}</ion-card-title>
<ion-card-subtitle id ="points">{{user.points}}</ion-card-subtitle>
</ion-card-header>
</ion-card>
</div>
</ion-content>
到目前为止,.ts返回用户ID。
任何帮助将不胜感激!!谢谢。
答案 0 :(得分:0)
代替
const ref = usersRef.orderByKey();
使用
const ref = usersRef.orderByChild('points');
在您的查询中。
您的角度着火代码将像这样更新:
this.users = this.db.list('/users', ref => ref.orderByChild('points')).valueChanges()