0

How do I add a uniqueId for each entry to Firebase on a push? Right now my code looks like:

      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)] 
                     })

I'm using AngularFire2 and grabbing a database reference to 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');
}

How do I push to the DB and instead of getting a list that looks like this with randomly generated Id's:

enter image description here

I get a list of intentional Id's that I have knowledge of and can select in the client code?

6
  • What do you mean by "have knowledge of"? Is there something in particular you want to do with the IDs before or after the push? Anyway, the push IDs are not entirely random, their initial component is a timestamp: gist.github.com/mikelehen/3596a30bd69384624c11
    – cartant
    Commented Jan 31, 2017 at 1:50
  • o really? yes the wording is a bit confusing there, I want to give them Id's like: 331, 123 or 554 rather than: KbaZqVQBp580Zzt4Uty and the others in that list Commented Jan 31, 2017 at 1:55
  • You can just add them using set or update with any ID you like as the key, but the onus is then on you to ensure the keys are unique. Typically, the use of push is encouraged for that reason.
    – cartant
    Commented Jan 31, 2017 at 2:06
  • I'm ok to use push, but how would I query for something within the list of games without using KbaZqVQBp580Zzt4Uty? Commented Jan 31, 2017 at 2:11
  • You can query using any of the child values: game_name, first_name, etc. Is that what you mean?
    – cartant
    Commented Jan 31, 2017 at 2:14

1 Answer 1

1

There are several ways you can do this:

Get the push-id and store it in another node :

There is a way to get the current push-id in AngularFire push method. So you can change the "randomly generated ids" to the ids that you "have knowledge of".

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);
})

Other than this, you can actually change the push key by using the set method instead of 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)]
    })

Where af contains the injected AngularFire module

 constructor(public af : AngularFire) { }

Even though this method works fine, it gives you another task of creating the unique ids, a task that firebase can do for you from the first method.

Also, as mentioned in the comments above, you can query your list by using the random_id child node.

Not the answer you're looking for? Browse other questions tagged or ask your own question.