【问题标题】:How to retrieve firebase database values into List using AngularFire如何使用 AngularFire 将 firebase 数据库值检索到列表中
【发布时间】:2018-12-03 17:42:00
【问题描述】:
这是我的 firebase 数据库的结构:
我正在尝试检索姓名列表,然后单击按钮通过电子邮件发送。
我目前的代码:
toemail(){
this.db.list('profiles').snapshotChanges().subscribe(
res => {
res.forEach(doc => {
this.people.push(doc.payload.val());
});
});
console.log(this.people);
}
此代码将所有数据(电子邮件、游戏、姓名)添加到人员列表中。如何修改它以仅将 game=1 的人的姓名添加到人员列表中?
谢谢
【问题讨论】:
标签:
firebase
firebase-realtime-database
ionic3
angularfire2
angularfire
【解决方案1】:
我建议您首先为您的数据创建一个模型,因为这将真正有助于保持您的代码的清洁和可读性以及利用 linting
这是一些修改后的代码,可能会有所帮助
toemail() {
this.db.list('profiles').snapshotChanges().subscribe(
res => {
this.people = []; //Reset the array every time data changes
this.people = res.filter(doc => {
let person = doc.payload.val() as Person;
//Person is the data model. Although you can omit this part if you wish and the code will still work
return person.game === 1;
});
});
console.log(this.people);
}
过滤函数以一个数组作为输入,遍历每个实体后返回一个新数组,只有满足一定条件的实体才加入新数组
在您的情况下,如果 person.game ===1 添加实体