Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Cloud Function get inner child of child from list (nodeJs)

Trying to get all the 'Token' values from each child inside the Infos Child, when the database event onUpdate trigger ,but it seems I'm wrong any solutions? Table

   var allTokens = [];

    await admin.database().ref('/Roles/Livreurs').once("value", function (snapshot) {

        snapshot.forEach(function (childSnapShot) {

            childSnapShot.forEach(function (innerChild) {
                var token = innerChild.val().Token;
                allTokens.push(token);
            });
        });
    });
like image 813
David.C Avatar asked Dec 04 '25 17:12

David.C


1 Answers

You only need forEach if you don't know the keys at a level in your JSON, in your case that only applies to the user IDs. For any level where you know the key, you can use child(thatkey) to get at that child snapshot.

So I think you're looking for:

let allTokens = await admin.database().ref('/Roles/Livreurs').once("value", function (snapshot) {
    var result = [];
    snapshot.forEach(function (userSnapShot) {
        var token = userSnapShot.child("Infos/Token").val();
        result.push(token);
    });
    return result;
});

And then use it as

like image 194
Frank van Puffelen Avatar answered Dec 06 '25 09:12

Frank van Puffelen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!