0

I have a little problem regarding Firebase and retrieving names of parent keys. Like in this example I want to get the names of the keys, not their values:

quick sample of what I want to archieve

Pls have a look in my db how it looks now, its exactly the same like in the JS sandbox:

enter image description here

When I try to implement this in my react-native project, basicly with plain JS in this part, it looks like this:

const sendletter = () => { console.log("letter send"); firebase.database(). refFromURL("https://SOMEDATABASE.firebaseio.com/numers"). once("value").then(snapshot => { for(key in snapshot){ console.log(key) } }) }; 

The problem I just face now it the outcome of the console in my IDE:

null letter send node_ ref_ index_ val exportVal toJSON exists child hasChild getPriority forEach hasChildren key numChildren getRef ref

I tried to provide you as many pictures so the problem gets really clear and also my goal I want to archieve, to get the key names itself, NOT the values the keys are linked to. Any help would be appriciated!

1 Answer 1

1

You want to use snapshot.val() instead of just the DataSnapshot object

for(key in snapshot.val()){ console.log(key); } 

When you are looping through just the snapshot variable, you are looking at all the keys in that DataSnapshot that is returned from firebase and not your actual data. If you check out Firebase docs, you'll see what the DataSnapshot object represents and how to actually get your data from the database FROM their DataSnapshot object that is returned: https://firebase.google.com/docs/reference/node/firebase.database.DataSnapshot

If you look close enough you can see that all the methods and values that this DataSnapshot object contains is actually what was being printed in your console all along

Sign up to request clarification or add additional context in comments.

2 Comments

This worked, thank you! Can you maybe explain shortly why snapshot.val() in this case is the correct answer?
There you go, i added an explanation

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.