Tommy John
Tommy John

Reputation: 84

How do I target the child of a certain Node in firebase?

I have a database that is structured with the following:


Users
-User1
--Buisiness_Name:
--Buisiness_Role:
--Description:
--Name:
--Phone_Number


I have the following code:

var ref = firebase.database().ref("users/" + user_id);
 firebase.database().ref().on('value', function(snapshot) {
   $'#bname').val((snapshot.val() && snapshot.val()));
   console.log(snapshot.val());
 });

How do I target only a certain child of the users/ directory with snapshot?

If my question is unclear, I will be glad to edit my question to improve it.

I need to also create a URL for every registered user.

Upvotes: 0

Views: 181

Answers (2)

Joseph Webber
Joseph Webber

Reputation: 2173

If you are trying to get just a single property like Business_Name:

var ref = firebase.database().ref("users/" + user_id + "/Business_Name");
//↓ You created the reference but didn't use it
ref.on("value", function(snapshot) {
  //↓ you forgot the "(" here
  $('#bname').val((snapshot.val());
});

If you are trying to get the entire document:

var ref = firebase.database().ref("users/" + user_id);
ref.on("value", function(snapshot) {
  $('#bname').val((snapshot.val().Business_Name);
});

More info on getting, setting and updating data can be found in the firebase docs.

Upvotes: 2

Mike Yan
Mike Yan

Reputation: 1699

If you want to get the data of 'Phone_Number', you can get the value by:

snapshot.val().Phone_Number

If you only want to get a single node data, like 'Phone_Number', you can also point the ref directly into that node.

var Phone_NumberRef = firebase.database().ref("users/" + user_id + "/Phone_Number");
Phone_NumberRef.on('value', function(snapshot) {
  console.log(snapshot.val());
});

Upvotes: 2

Related Questions