Friday, February 17, 2017

Firebase - Write List Data

In our last chapter we showed you how to write data in FIrebase. Sometimes you need to have unique identifier for your data.
When you want to create unique identifiers for your data, you need to use push instead of set.

Push

The push() method will create unique id when the data is pushed. If we want to create our players from the last chapters with unique id, we could use the code snippet below.
var ref = new Firebase('https://tutorialsfirebase.firebaseio.com');

var playersRef = ref.child("players");
playersRef.push({
   name: "John",
   number: 1,
   age: 30
});

playersRef.push({
   name: "Amanda",
   number: 2,
   age: 20
});
Now our data will look differently. The name will just be a name/value pair like the rest of the properties.
Firebase Write List Data Push

Key

We can get any key from Fireabse by using key() method. For example, if we want to get our collection name, we could use the following snippet.
var ref = new Firebase('https://tutorialsfirebase.firebaseio.com');

var playersRef = ref.child("players");

var playersKey = playersRef.key();
console.log(playersKey);
The console will log our collection name (players);
Firebase Write List Data Key More on this in our next chapters.

No comments:

Post a Comment