Benny
Benny

Reputation: 498

Fetching a key of a current value from Firebase Realtime Database

I have this simple Firebase Realtime Database:

enter image description here

and I have the inner Id (in blue), How Can I Fetch the key (in red) using ReactJS?

I've already read the Firebase Realtime Database Docs (Modular SDK v9) but I did not see a way to do so. I'll appreciate any help.

Upvotes: 0

Views: 151

Answers (1)

Tarik Huber
Tarik Huber

Reputation: 7418

Some basic code if you want to get the whole list for ID 1 would look like this:

import { getDatabase, get, ref } from "firebase/database";

const getData = async () => {
  const db = getDatabase();
  const snap = await get(ref(db, `Lists/${id1}`));

  snap.forEach((r) => {
    console.log("key", r.key);
    console.log("value", r.val()); // red
    console.log("id", r.val()["id"]); // blue
  });
};

Upvotes: 1

Related Questions