retrieve data from firebase realtime database code example
Example 1: used to retrieve dat from firebase realtime datastore
var playersRef = firebase.database().ref("players/");
playersRef.on("child_added", function(data, prevChildKey) {
var newPlayer = data.val();
console.log("name: " + newPlayer.name);
console.log("age: " + newPlayer.age);
console.log("number: " + newPlayer.number);
console.log("Previous Player: " + prevChildKey);
});
Example 2: firebase realtime database store array
The Firebase Database doesn't store arrays. It stores dictionaries/associate arrays. So the closest you can get is:
attendees: {
0: "Bill Gates",
1: "Larry Page",
2: "James Tamplin"
}
Example 3: firebase realtime database read data
If you want to get a specific data using a phone number then you can use a Query like this
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = database.child("profiles");
Query phoneQuery = ref.orderByChild(phoneNo).equalTo("+923336091371");
phoneQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
user = singleSnapshot.getValue(User.class);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "onCancelled", databaseError.toException());
}
});
I think this is the best method.