gpt4 book ai didi

reactjs - Firebase 错误 : No firebase app default has been created

转载 作者:行者123 更新时间:2023-12-04 14:06:04 25 4
gpt4 key购买 nike

我正在使用 react-native 和 redux 制作食品配送应用程序。我想从 firebase 存储中获取数据,为此,我在 中编写了一个函数行动 .js,但每当我运行应用程序时,它都会显示错误Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app).这是我用来获取数据的函数
action.js

import firebase from "firebase"
export const ProductDetails = (data) => {
return {
type: "PRODUCT_ITEMS",
payload: {
data,
},
};
};

var db = firebase.firestore();
var docRef = db.collection("Products").doc("Items");
export const FetchItems = () => {
return async function (dispatch) {
return await docRef
.get()
.then((doc) => {
if (doc.exists) {
console.log("Document Data", doc.data());
dispatch(ProductDetails(doc.data));
} else {
console.log("NO such document");
}
})
.catch((error) => {
console.log(error);
});
};
};

这是我的 App.js 文件
import React, { useState } from "react";
import { StyleSheet, Text, View, Dimensions } from "react-native";
import { NavigationContainer } from "@react-navigation/native";
import AppStack from "./components/navigation/AppStack";

import firebase from "firebase";
import {Provider} from "redux"
import store from "./store"

import { useDispatch, useSelector, Provider } from "react-redux";
import store from "./redux/store";
import AppWrapper from "./AppWrapper";

export default function App() {

const firebaseConfig = {
};
if (firebase.apps.length === 0) {
firebase.initializeApp(firebaseConfig);
}
return (
<Provider store={store}>
<NavigationContainer>
<AppStack />
</NavigationContainer>
</Provider>
);;
}


最佳答案

我建议创建一个单独的文件 firebase.js并在初始化后从那里导出 Firebase 服务。
firebase.js

import firebase from 'firebase/app';
import 'firebase/firestore'

const firebaseConfig = {...};

if (!firebase.apps.length) {
firebase.initializeApp(config);
}

export const auth = firebase.auth();
export const firestore = firebase.firestore()
然后在任何需要它们的地方导入它们:
// App.js for example

import {firestore, auth} from './firebase.js'

//var docRef = firestore.collection("Products").doc("Items");

关于reactjs - Firebase 错误 : No firebase app default has been created,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68470035/

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