Gurmukh Singh
Gurmukh Singh

Reputation: 2017

Retrieve Firebase Object id

Im using the following loop to get data from a table which is working fine:

database.once("value", function(snapshot){
  snapshot.forEach(snap => {
    log.console(snap.val().about_me)
})

Its displaying the data to logs as expected, But i can't seem to get the object id?

I need to get the value 49GRZb8B31MUfpBN3zvHKOHCMOa2

enter image description here

I have tried:

snap.val().key; 

This returns undefined

And have tried snap.val()[0];

which returns me everything in the object

Upvotes: 0

Views: 242

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317467

snap is a DataSnapshot type object. You can see that it has a property called key. So, you need to reference that property directly on the DataSnapshot like this:

snap.key  // correct

But not via the raw data object like this:

snap.val().key   // incorrect, you have no child named 'key' in your data

Upvotes: 1

Related Questions