Skip to content Skip to sidebar Skip to footer

Read Only Certain Fields In Firebase

If I have a database like users: { user1: { name:'qwert', id: 123, num: 9999 }, user2: { name:'zxcvb', id: 456, num: 8888 } } How can I read

Solution 1:

There is no way to select only the names from your current data model, Firebase always retrieves full nodes. So you will either have to do what Peter answered, reading all data but extracting the user name client side, or you will have to modify you data model to allow the use-case.

The latter is quite simple. If you want to load a list of user names, you should store a list of user names in the database:

users: {
  user1: { 
    name:'qwert', 
    id:123,
    num:9999 
  },
  user2: { 
    name:'zxcvb', 
    id:456,
    num:8888 
  }
},usernames: {
  user1:'qwert', 
  user2:'zxcvb'
}

With this structure, it is trivial to read only the names.

Also see:

Solution 2:

To retrieve only the names of the users, then try the following:

firebase.database().ref().child("users").on("value", function (snapshot) {
  snapshot.forEach(function(childSnapshot) {
   var name=childSnapshot.val().name;
  });
});

Here the snapshot is users, then you iterate inside user1 and user2 to be able to access the names of these users and retrieve them.

Post a Comment for "Read Only Certain Fields In Firebase"