Firebase:使用 AngularFire2 推送到嵌入对象中的列表
Firebase: Push to list embedded in an object with AngularFire2
我正在尝试弄清楚如何使用 AngularFire2 将值附加到 Firebase 对象中的列表。用例是创建一个简单的记分板,每次通过将以下 Score
对象推送到列表来更改分数时都会保存分数:
{
points: [15, 11],
sets: [2, 1],
timestamp: new Date()
}
数据库中的一个 Scoreboard
对象如下所示:
{
event: 'Volleybal',
home: 'Londen',
away: 'Manchester',
scores: [Score]
}
其中 scores
数组是 Score
对象的数组,如上所述。我需要能够执行两项任务:
- 查询 Firebase 数据库中的
Scoreboard
列表以获得正确的事件(假设事件是唯一的)。
- 每次分数变化时,通过推送新的
Score
对象来更新 scores
数组。
这是正确的架构设计吗?我如何使用 AngularFire2 执行这些任务?
上面的架构似乎涵盖了您的用例。我建议 Scoreboard
对象的 scores
属性 在您的代码中作为对象与数组进行处理(并存储)。
假设 event
属性 对于所有 Scoreboard
对象都是唯一的,您可以使用类似下面的内容从 Firebase 检索它。
const event = 'Volleyball';
const scoreboards = af.database.list('scoreboards', {
query: {
orderByChild: 'event',
equalTo: 'large'
}
});
但是,如果您在对象中有一个唯一键,则可能值得考虑将该键用于 Scoreboard
对象本身,因此记分板资源将如下所示
{
'Volleyball': {
home: 'London',
away: 'Manchester',
scores: {}
},
'Football': {
home: 'London',
away: 'Manchester',
scores: {}
},
...
}
这样做可以让您 retrieve/update 像下面这样的对象。
// Get Scoreboard
const event = 'Volleyball';
const scoreboard = af.database.object('scoreboards/' + event);
// Add a new score to the scores property of Scoreboard
af.database.list('/scoreboards/' + event + '/scores').push({
points: [15, 11],
sets: [2, 1],
timestamp: new Date()
});
值得注意的是,Firebase 实际上并不存储数组;如果您向 Firebase 提供一个数组,它会将它变成一个对象,键是数组的索引。 https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html
根据下面的评论编辑答案
要显示最新的存储值,您可以使用类似下面的方法获取该值。
const event = 'Volleyball';
const scoreboard = af.database.list('/scoreboards/' + event + '/scores', {
query: {
orderByChild: 'timestamp',
limitToLast: 1
}
});
scoreboard.subscribe(list => {
list.forEach(score => {
// Can access the values of the Score object here
console.log(score.points);
});
});
我正在尝试弄清楚如何使用 AngularFire2 将值附加到 Firebase 对象中的列表。用例是创建一个简单的记分板,每次通过将以下 Score
对象推送到列表来更改分数时都会保存分数:
{
points: [15, 11],
sets: [2, 1],
timestamp: new Date()
}
数据库中的一个 Scoreboard
对象如下所示:
{
event: 'Volleybal',
home: 'Londen',
away: 'Manchester',
scores: [Score]
}
其中 scores
数组是 Score
对象的数组,如上所述。我需要能够执行两项任务:
- 查询 Firebase 数据库中的
Scoreboard
列表以获得正确的事件(假设事件是唯一的)。 - 每次分数变化时,通过推送新的
Score
对象来更新scores
数组。
这是正确的架构设计吗?我如何使用 AngularFire2 执行这些任务?
上面的架构似乎涵盖了您的用例。我建议 Scoreboard
对象的 scores
属性 在您的代码中作为对象与数组进行处理(并存储)。
假设 event
属性 对于所有 Scoreboard
对象都是唯一的,您可以使用类似下面的内容从 Firebase 检索它。
const event = 'Volleyball';
const scoreboards = af.database.list('scoreboards', {
query: {
orderByChild: 'event',
equalTo: 'large'
}
});
但是,如果您在对象中有一个唯一键,则可能值得考虑将该键用于 Scoreboard
对象本身,因此记分板资源将如下所示
{
'Volleyball': {
home: 'London',
away: 'Manchester',
scores: {}
},
'Football': {
home: 'London',
away: 'Manchester',
scores: {}
},
...
}
这样做可以让您 retrieve/update 像下面这样的对象。
// Get Scoreboard
const event = 'Volleyball';
const scoreboard = af.database.object('scoreboards/' + event);
// Add a new score to the scores property of Scoreboard
af.database.list('/scoreboards/' + event + '/scores').push({
points: [15, 11],
sets: [2, 1],
timestamp: new Date()
});
值得注意的是,Firebase 实际上并不存储数组;如果您向 Firebase 提供一个数组,它会将它变成一个对象,键是数组的索引。 https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html
根据下面的评论编辑答案 要显示最新的存储值,您可以使用类似下面的方法获取该值。
const event = 'Volleyball';
const scoreboard = af.database.list('/scoreboards/' + event + '/scores', {
query: {
orderByChild: 'timestamp',
limitToLast: 1
}
});
scoreboard.subscribe(list => {
list.forEach(score => {
// Can access the values of the Score object here
console.log(score.points);
});
});