将唯一 ID 添加到 Firebase
Add Unique Id to Firebase
如何在推送时为 Firebase 的每个条目添加一个 uniqueId?现在我的代码看起来像:
this.games.push({
game_name: data.gameName,
number_of_players: data.number_of_players,
random_id: this.randomId,
admin: data.admin,
first_name: data.your_name,
player_array: [this.combinePlayerName(this.firstName, this.admin)]
})
我正在使用 AngularFire2 并获取对 this.games 的数据库引用:
constructor(public navCtrl: NavController, public alertCtrl: AlertController, af: AngularFire) {
this.games = af.database.list('/games');
this.ids = af.database.list('/games/random_id');
}
我如何推送到数据库而不是得到一个看起来像这样的带有随机生成的 ID 的列表:
我得到了一份我知道并且可以在客户端代码中 select 的有意 ID 列表?
您可以通过多种方式执行此操作:
获取push-id并将其存储在另一个节点中:
在 AngularFire 推送方法中有一种获取当前 push-id 的方法。因此,您可以将 "randomly generated ids" 更改为您 "have knowledge of".
的 ID
this.games.push({
game_name: data.gameName,
number_of_players: data.number_of_players,
random_id: this.randomId,
admin: data.admin,
first_name: data.your_name,
player_array: [this.combinePlayerName(this.firstName, this.admin)]
}).then((game) => {
this.ids.push(game.key);
})
除此之外,您实际上可以使用 set 方法而不是 push 来更改 push 键:
this.af.database.object("games/"+this.randomId).set({
game_name: data.gameName,
number_of_players: data.number_of_players,
random_id: this.randomId,
admin: data.admin,
first_name: data.your_name,
player_array: [this.combinePlayerName(this.firstName, this.admin)]
})
其中 af 包含注入的 AngularFire 模块
constructor(public af : AngularFire) { }
尽管此方法工作正常,但它为您提供了创建唯一 ID 的另一项任务,firebase 可以从第一种方法开始为您完成这项任务。
此外,如上面评论所述,您可以使用 random_id 子节点查询您的列表。
如何在推送时为 Firebase 的每个条目添加一个 uniqueId?现在我的代码看起来像:
this.games.push({
game_name: data.gameName,
number_of_players: data.number_of_players,
random_id: this.randomId,
admin: data.admin,
first_name: data.your_name,
player_array: [this.combinePlayerName(this.firstName, this.admin)]
})
我正在使用 AngularFire2 并获取对 this.games 的数据库引用:
constructor(public navCtrl: NavController, public alertCtrl: AlertController, af: AngularFire) {
this.games = af.database.list('/games');
this.ids = af.database.list('/games/random_id');
}
我如何推送到数据库而不是得到一个看起来像这样的带有随机生成的 ID 的列表:
我得到了一份我知道并且可以在客户端代码中 select 的有意 ID 列表?
您可以通过多种方式执行此操作:
获取push-id并将其存储在另一个节点中:
在 AngularFire 推送方法中有一种获取当前 push-id 的方法。因此,您可以将 "randomly generated ids" 更改为您 "have knowledge of".
的 IDthis.games.push({
game_name: data.gameName,
number_of_players: data.number_of_players,
random_id: this.randomId,
admin: data.admin,
first_name: data.your_name,
player_array: [this.combinePlayerName(this.firstName, this.admin)]
}).then((game) => {
this.ids.push(game.key);
})
除此之外,您实际上可以使用 set 方法而不是 push 来更改 push 键:
this.af.database.object("games/"+this.randomId).set({
game_name: data.gameName,
number_of_players: data.number_of_players,
random_id: this.randomId,
admin: data.admin,
first_name: data.your_name,
player_array: [this.combinePlayerName(this.firstName, this.admin)]
})
其中 af 包含注入的 AngularFire 模块
constructor(public af : AngularFire) { }
尽管此方法工作正常,但它为您提供了创建唯一 ID 的另一项任务,firebase 可以从第一种方法开始为您完成这项任务。
此外,如上面评论所述,您可以使用 random_id 子节点查询您的列表。