我如何读取 firebase 实时数据库?

how can i read firebase realtime database?

这是我的代码,它是文档的精确副本,但它说 ref.on 不是函数。也许我忘了在导入中放入一个模块,但我找不到任何相关信息。

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

这里是导入行:

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

您正在导入新的 v9 函数,但您随后在代码中尝试使用旧语法。您必须选择其中之一。

在 v9 语法中,ref.on("value" 将是:

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

另请参阅 reading from the Realtime Database with the v9 SDK 上的 Firebase 文档。