简体   繁体   中英

How to read data from Firebase Real Time database with React Native?

I have a really simple problem, that when I try to read data from the FireBase database, with the following code I get this error: "TypeError: ref is not a function". I dig trough a lot of documentation and they all use it like this, but I do not have a function like that for my ref variable. I want to query all the animal names for a user ('AllatNev').

import { getDatabase, ref, set } from 'firebase/database';

const db = getDatabase();
const user_ref = ref(db, auth.currentUser.uid); //This returns my db and the userID
const query = user_ref.child("AllatNev")        //This fails with "no child function"
console.log(query)

I can easily write data with this code:

  const reference = ref(db, auth.currentUser.uid +"/"+allatid);
  set(reference, {
    'AllatNev': AllatNev,
    'AllatFaj': valueFaj,
    'AllatFajta': AllatFajta,
    'AllatSzin': AllatSzin,
    'AllatNem': valueNem,
    'AllatSzul': dateText
  });

My data looks like this:

{
  "Gkmo3hhjNaZJ8yzjHcLKAyA3Op12" : {
    "allatid1041301e703d1" : {
      "AllatFaj" : "macska",
      "AllatFajta" : "Egyiptomi",
      "AllatNem" : "szuka",
      "AllatNev" : "Zolika",
      "AllatSzin" : "Fekete",
      "AllatSzul" : "2020-4-18"
    },
    "allatid53ba6d927a183" : {
      "AllatFaj" : "kutya",
      "AllatFajta" : "Border Collie",
      "AllatNem" : "kan",
      "AllatNev" : "Grafit",
      "AllatSzin" : "Kék",
      "AllatSzul" : "2016-4-18"
    }
  }
}

This code can't work as you're using v8 syntax:

const query = user_ref.child("AllatNev")

Since you're using SDK version 9 or later, you should also use the syntax for that version to get the child node:

const query = child(user_ref, "AllatNev")

I always keep the list of v9 database functions handy when working on code like this, as it's the fastest way to find syntax problems like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM