1

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" : "______"
      }
    }
}
}
Unaidu
  • 73
  • 1
  • 7
  • If it's possible, it'll be through a [query](https://firebase.google.com/docs/database/web/lists-of-data#sorting_and_filtering_data). It's hard to say more without seeing your data structure. So if the documentation isn't enough, add your JSON structure to the question (as text, no screenshots please). You can get this by clicking the "Export JSON" link in the overflow menu (⠇) on your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Jul 28 '19 at 22:20
  • @FrankvanPuffelen I just added that JSON structure to the question. – Unaidu Jul 29 '19 at 07:57

2 Answers2

1

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);
  })
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I have tried this but nothing happens in the console. – Unaidu Jul 29 '19 at 20:10
  • Yeah, I now realize you have *two* dynamic levels under `products`, which won't work. Firebase queries work on a flat list. See https://stackoverflow.com/questions/27207059/firebase-query-double-nested – Frank van Puffelen Jul 30 '19 at 04:46
0

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);  
 });
James Palfrey
  • 753
  • 6
  • 29
  • I have written the following but it returns 'null'. `var this = this.id; var thisDbData = firebase.database().ref('products/{}/' + this); thisDbData.on('value', function(snapshot){ var name = snapshot.child('name').val(); console.log(name); });` – Unaidu Jul 29 '19 at 08:27
  • firebase.database().ref('products/{userName}/{pushId}/name'); use this. – James Palfrey Jul 29 '19 at 08:36
  • It keeps returning ''null". If you write "..../name" inside ref(), then inside the function I would have to use `snapshot.val()` instead of `snapshot.child('name').val()`, right? I have tried with both and they return "null". – Unaidu Jul 29 '19 at 08:46