Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase get key value

It's easy to get value:

    firebase_instance.limit(100).on("child_added", function(data) {
        var value = data.val();
    });

but how to get a key associated with that value?

like image 587
good_evening Avatar asked Dec 08 '22 09:12

good_evening


2 Answers

Use key property

firebase_instance.limit(100).on("child_added", function(snap) {
  var name = snap.key;
});

Documentation and examples here

like image 169
VictorC Avatar answered Dec 10 '22 22:12

VictorC


Each of your Firebase .on(...) callbacks are going to be invoked with a Firebase DataSnapshot. To get the name for the location of that DataSnapshot, try using the name() method.

For example:

firebase_instance.limit(100).on("child_added", function(snap) {
  var name = snap.name();
});
like image 34
Rob DiMarco Avatar answered Dec 10 '22 23:12

Rob DiMarco