Saeed Heidarizarei
Saeed Heidarizarei

Reputation: 8916

Access to Deep data via Firebase Admin

How Can I Access to Deep data via Firebase Admin?

Data:

{
    "keyboards": {
        "StartKeyboard": [
            "KeyboardA",
            "KeyboardB",
            "KeyboardC"
        ],
        "SecendKeyboard": {
            "parent": "StartKeyboard",
            "childs": [      //*** I need to get this childs: [] ***
                "Keyboard1",
                "Keyboard2",
                "Keyboard3"
            ]
        }
    }
}

When I use below Code, I have all data in output

const ref = db.ref('/');    All Data
ref.on("value", function (snapshot) {
    console.log(snapshot.val());
  });

When I use below Code, I have childs of keyboards in output

 const ref = db.ref('keyboards');   // inside of Keyboards
    ref.on("value", function (snapshot) {
        console.log(snapshot.val());
      });

But I Don't Know How to get childs of SecendKeyboard/childs. I mean array of Keyboard1 and Keyboard2 and Keyboard3 . Thank you.

Upvotes: 0

Views: 52

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599341

To get the keyboard children:

const ref = db.ref('keyboards/SecendKeyboard/childs');
ref.on("value", function (snapshot) {
    console.log(snapshot.val());
});

Or:

const ref = db.ref('keyboards/SecendKeyboard');
ref.on("value", function (snapshot) {
    console.log(snapshot.child("childs").val());
});

Or

const ref = db.ref('keyboards');
ref.on("value", function (snapshot) {
    snapshot.forEach(function(childSnapshot) {
        console.log(snapshot.val()); // prints StartKeyboard and SecendKeyboard
        if (snapshot.child("SecendKeyboard").exists()) {
            console.log(snapshot.child("SecendKeyboard").val());
        }
    })
});

Upvotes: 1

Related Questions