Unaidu
Unaidu

Reputation: 73

How can I retrieve the data of a node without having its parents node's value/name?

I would like to retrieve the data of a node which is inside some other nodes (in the firebase database) without having those parents nodes's names. How can I do it? Thank you.

This is what I have tried. ("this" in the database is inside some other nodes).

 var this = this.id;
 var thisDbData = firebase.database().ref(this);
 thisDbData.on('value', function(snapshot){
   var name = snapshot.child('name').val();
   console.log(name);  
 });

The "name" variable returns null. This is the database structure:

{
"products" : { //I have access to this name
    "W9bgPeMeYieEvP2FGb9ZOvvhx0T2" : { //I don't have access to this name
      "-Lh0CbgAW8R2-vjMELqr" : { //I have access to this name
        "description" : "________", //What I wanna get
        "name" : "_______" //What I wanna get
      },
      "-Lh0IqYJeS91dM6Qurye" : {
        "description" : "______",
        "name" : "______"
      }
    }
}
}

Upvotes: 1

Views: 241

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 599111

You should be able to use orderByKey() to get the node by its key:

var root = firebase.database().ref('products');
var query = root.orderByKey().equalTo('-Lh0CbgAW8R2-vjMELqr');
query.once('value', function(snapshot){
  snapshot.forEach(function(child) {
    var name = child.child('name').val();
    console.log(name);
  })
});

Upvotes: 1

James Palfrey
James Palfrey

Reputation: 803

you should be able to use context.params to get these names if you need them otherwise using {} allows you to access sub nodes without knowing what they are.

var this = this.id;
 var thisDbData = firebase.database().ref("products/{userName}/{pushId}/name");
 thisDbData.on('value', function(snapshot){
   var name = snapshot.val();
   return console.log(name);  
 });

Upvotes: 0

Related Questions