0

enter image description here

On the left my database schema, on the right is my react-native code.

I followed this documentation to get started with Realtime Database for Android.

I am trying to get the value "Lesson" from key "Category". none of the commands I tried work. How I can get Lesson value? I tried test.parent but I got the correct location in my database but I want the values.

thanks!

1 Answer 1

1

Documentation is your Friend!

The answer to your question is in the document you attached! If you scroll a bit down there's a section One-time read

To read the value once, call the once method on a reference:

import database from '@react-native-firebase/database';

database()
  .ref('/users/123')
  .once('value')
  .then(snapshot => {
    console.log('User data: ', snapshot.val());
  });

So just replace the ref path '/users/123' with your path 'Lesson 1/1 Introduction/Category'

You can also listen for changes in the document in real-time. Check out Real-Time Changes

import database from '@react-native-firebase/database';

database()
  .ref('/users/123')
  .on('value', snapshot => {
    console.log('User data: ', snapshot.val());
  });
Sign up to request clarification or add additional context in comments.

4 Comments

is there a simpler approach that doesn't involve the snapshot methods?
I want to try and save the value '1 Introduction' and all of the other children that might be on the same level into an array
What do you mean by "simpler approach" ? Since you need all objects on the 1 Introduction level, you should set the ref path as Level 1. If you want to read from the DB only 1 time, then use the first method, when promise resolves, set it onto any state variable
Thanks I was able to get what I needed like follows: ` const reference = database() .ref() .once('value') .then(snapshot => { //get Levels from db let values = []; snapshot.forEach((child) => { values.push(child.key); }) console.log(values);`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.