简体   繁体   中英

how can i read firebase realtime database?

here's my code, it is the exact copy of the docs but it says that ref.on is not a function. maybe i forgot to put a module in the import but i couldn't find any information about that.

function readpixels() {
  const db = getDatabase();
  const ref = ref(db, 'pixels');
  ref.on('value', (pixel) => {
    console.log("read")
  })
}

and here are the import lines:

import { initializeApp } from "firebase/app";
import { getDatabase, ref, push, set } from "firebase/database";

You're importing the new v9 functions, but in your code you are then trying to use the older syntax. You'll have to pick one or the other.

In v9 syntax, the ref.on("value" would be:

import { onValue } from "firebase/database";
...
onValue(ref, (pixel) => {
  console.log("read")
}

Also see the Firebase documentation on reading from the Realtime Database with the v9 SDK .

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