gpt4 book ai didi

javascript - firebase.database() 不是 react 中的函数错误

转载 作者:行者123 更新时间:2023-12-03 08:16:56 32 4
gpt4 key购买 nike

为了提供一些背景信息,我正在尝试使用 redux-toolkit 和 react-redux-firebase 设置我的 React 应用程序(需要 firebase 实时数据库)。

我按照react-redux-firebase自述文件创建了这个文件


import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import { store } from "./app/store";
import { Provider } from "react-redux";
import * as serviceWorker from "./serviceWorker";
import { initializeApp } from "firebase/app";
import "firebase/database"; // to enable using firebase RTD
import { ReactReduxFirebaseProvider } from "react-redux-firebase";

const fbConfig = {
apiKey: "**",
authDomain: "**",
projectId: "**",
storageBucket: "**",
messagingSenderId: "**",
appId: "**",
measurementId: "**",
databaseURL: "firebase-rtdb-url**",
};

let firebase = initializeApp(fbConfig);

const rrfConfig = {
userProfile: "users",
};

const rrfProps = {
firebase,
config: rrfConfig,
dispatch: store.dispatch,
};

ReactDOM.render(
<React.StrictMode>
<Provider store={store}>
<ReactReduxFirebaseProvider {...rrfProps}>
<App />
</ReactReduxFirebaseProvider>
</Provider>
</React.StrictMode>,
document.getElementById("root")
);

store.js 只是使用 redux 工具包来创建和导出商店。


import { configureStore } from "@reduxjs/toolkit";
import counterReducer from "../features/counter/counterSlice";
import { firebaseReducer } from "react-redux-firebase";

export const store = configureStore({
reducer: {
counter: counterReducer,
firebase: firebaseReducer,
},
});


并且还使用react-redux-firebase网站上给出的演示代码

import React from "react";
import { useFirebase } from "react-redux-firebase";

export default function Todos() {
const firebase = useFirebase();

function addSampleTodo() {
const sampleTodo = { text: "Sample", done: false };
return firebase.push("todos", sampleTodo);
}

return (
<div>
<h1>New Sample Todo</h1>
<button onClick={addSampleTodo}>Add</button>
</div>
);
}

当我点击“添加”按钮时,这是我得到的异常 Screenshot from 2021-09-07 23-31-08

如果需要其他任何内容来改进该问题,请告诉我。会感谢任何帮助吗?

提前致谢

最佳答案

由于您使用的是新的模块化 SDK,因此请确保将其用于所有 Firebase 服务:

import "firebase/database"; // Older name-spaced version

实时数据库的新模块化语法如下所示:

import { getDatabase } from "firebase/database" 

let firebase = initializeApp(fbConfig);

const db = getDatabase(firebase)

function addSampleTodo () {
// Get a key for a new ToDo.
const newTodoKey = push(child(ref(db), 'posts')).key;

return update(ref(db), { updates['/todos/' + newTodoKey]: todoData });
}

您可以在 documentation 中找到更多详细信息。 。如果您使用 Firebase JS SDK v9+,则此设置有效。


或者,您可以将导入更改为兼容版本以继续使用现有语法。

import firebase from "firebase/compat/app"
import "firebase/compat/database"
// import "firebase/compat/[SERVICE_NAME]"

// Also use the name-spaced syntax everywhere else then
firebase.initializeApp(fbConfig);

关于javascript - firebase.database() 不是 react 中的函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69092798/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com