无法从Firebase真实的数据库检索数据

kpbwa7wx  于 2022-12-24  发布在  其他
关注(0)|答案(2)|浏览(129)

I am trying to retrieve data from a firebase real-time database and I am following this tutorial from the firebase documentation: https://firebase.google.com/docs/database/admin/retrieve-data#section-start. For my front-end framework I am using Svelte.
我在firebase.js中设置了数据库。

火线. js

// Import the functions you need from the SDKs you need
import { initializeApp } from 'firebase/app';
import { getDatabase } from 'firebase/database';
import { getAuth, GoogleAuthProvider } from 'firebase/auth';

const firebaseConfig = {
    apiKey: "AIzaSyCHL9UcT3TtvgQwt7N3DbLjRon9gKPFjA0",
    authDomain: "lyrics-and-quotes.firebaseapp.com",
    databaseURL: "https://lyrics-and-quotes-default-rtdb.firebaseio.com",
    projectId: "lyrics-and-quotes",
    storageBucket: "lyrics-and-quotes.appspot.com",
    messagingSenderId: "492758193692",
    appId: "1:492758193692:web:60ab73db53010e7fa7b1d9",
    measurementId: "G-T33YSTR82R",
};

// Initialize Firebase
initializeApp(firebaseConfig);

// Initialize Realtime Database and get a reference to the service
export const db = getDatabase(app);

// Initialize auth
export const auth = getAuth();

// Initialize Google auth provider
export const googleProvider = new GoogleAuthProvider();

在我的一个前端组件上,我有:

import { db } from '../database/firebase';
    const postsRef = db.ref('posts');

但是,我得到了错误:Posts.svelte:5 Uncaught TypeError: db.ref is not a function.

rjjhvcjd

rjjhvcjd1#

firebase.js文件中未定义app
你需要做的

const app = initializeApp(firebaseConfig);
export const db = getDatabase(app);

如文档中所示。

vpfxa7rd

vpfxa7rd2#

看起来您从firebase导入的db对象没有ref方法。
为了访问ref方法,您需要从firebase包导入它,如下所示:

import firebase from 'firebase/app';
import 'firebase/database';

const postsRef = firebase.database().ref('posts');

确保您还在项目中安装了firebase和firebase/database包。
这将使您可以访问firebase包中的所有方法和函数,包括database方法和ref方法。
希望这能有所帮助!

相关问题