redux Firebase错误:未创建Firebase应用默认值

qlzsbp2j  于 11个月前  发布在  其他
关注(0)|答案(4)|浏览(197)

我正在使用react-native和redux制作一个食品配送应用程序。我想从firebase商店获取数据,为此,我在actions.js中编写了一个函数,但每当我运行应用程序时,它都会显示错误
第一个月
下面是我用来获取数据的函数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>
  );;
}

g6ll5ycj

g6ll5ycj1#

我建议创建一个单独的文件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");

pb3s4cty

pb3s4cty2#

对于使用expo(v44)和**firebase(v9)**的用户

*firebaseUtil.js

import { initializeApp, getApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
import { getAuth } from "firebase/auth";

// Initialize Firebase
const firebaseConfig = {
  ...
};

const app = initializeApp(firebaseConfig);

const db = getFirestore(app);
const auth = getAuth(app);

export { db, auth };

字符串

Login.js

import { auth } from "../../util/firebaseUtil";

export default function Login() {

  const onSignUp = () => {
    signInWithEmailAndPassword(auth, email, password);
  };

  return (
    ...
  )

}

6gpjuf90

6gpjuf903#

我认为我的错误是由Webpack分块( Bootstrap )引起的。我确实用initializeApp()导入了文件。我的工作是为了谁有同样的问题。
1.将initializeApp模块化到一个文件中(对我来说是initFire)并导出任何内容(不一定是app
1.在第一次使用getApp()之前导入和导出(像getAuth()这样的方法将调用它),
1.这样,在Webpack之后,它仍然会首先运行。(ES6 export {app} from "./initFire"
(This答案来自my own GitHub Wiki,不是抄袭)

x4shl7ld

x4shl7ld4#

我最初将[FIRApp configure];放在application:didnessshLaunchingWithOptions中的return YES;之前。将configure移到这个方法的开头为我解决了这个问题。

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ [FIRApp configure];

字符串

相关问题