设置firebase时出错,可能会出现未处理的承诺拒绝

Posted

技术标签:

【中文标题】设置firebase时出错,可能会出现未处理的承诺拒绝【英文标题】:Error setting up firebase with react possible unhandled promise rejection 【发布时间】:2020-06-17 11:22:44 【问题描述】:

我目前正在创建一个食品订购系统,并且我正在尝试在一个 react-native 项目上设置 firebase

该应用程序允许我使用来自 firebase 的身份验证,我可以成功登录并注册新用户,但它不允许我使用 firestore 我正在尝试在下面的代码中进行控制台登录,以查看 firestore 是否将与应用程序一起运行

下面是 app.js/我从 firestore 调用集合的屏幕列表

错误是这样的

firebase error //android 模拟器读取错误

build.gradle 应用级别和

//用于react的包列表

package.json

"name": "projectFinal",
  "version": "0.0.1",
  "private": true,
  "scripts": 
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint ."
  ,
  "dependencies": 
    "@react-native-community/masked-view": "^0.1.7",
    "@react-native-firebase/app": "^6.3.4",
    "@react-native-firebase/auth": "^6.3.4",
    "@react-native-firebase/firestore": "^6.3.4",
    "firebase": "^7.9.3",
    "react": "16.9.0",
    "react-native": "0.60.5",
    "react-native-elements": "^1.2.7",
    "react-native-gesture-handler": "^1.6.0",
    "react-native-reanimated": "^1.7.0",
    "react-native-safe-area-context": "^0.7.3",
    "react-native-safe-area-view": "^1.0.0",
    "react-native-screens": "^2.2.0",
    "react-native-vector-icons": "^6.6.0",
    "react-navigation": "^4.2.2",
    "react-navigation-header-buttons": "^3.0.5",
    "react-navigation-stack": "^2.2.2",
    "react-navigation-tabs": "^2.8.2"
  ,
  "devDependencies": 
    "@babel/core": "^7.8.6",
    "@babel/runtime": "^7.8.4",
    "@react-native-community/eslint-config": "^0.0.7",
    "babel-jest": "^25.1.0",
    "eslint": "^6.8.0",
    "jest": "^25.1.0",
    "metro-react-native-babel-preset": "^0.58.0",
    "react-test-renderer": "16.9.0"
  ,
  "jest": 
    "preset": "react-native"
  


app.js


import React,  Component  from 'react';
import  View, StyleSheet, Text  from 'react-native';

import createAppContainer, createSwitchNavigator from 'react-navigation';
import createStackNavigator from 'react-navigation-stack';
import createBottomTabNavigator, createTabNavigator from 'react-navigation-tabs'
import Icon from 'react-native-vector-icons/Ionicons'
import  HeaderButtons, HeaderButton, Item  from 'react-navigation-header-buttons';

import Home from './src/Screens/Home'
 //import MyAccount from './Screens/MyAccount';
 import Register from './src/Screens/Register'
 import Login from './src/Screens/Login';
 //import Menus from './Screens/Menus';
import LoadingScreen from './src/Screens/LoadingScreen';
// import Jitters from './Screens/Jitters';
// import Scullery from './Screens/Scullery';
// import Bunker from './Screens/TheBunker';
// import OrderDetails from './Screens/OrderDetails';
import ViewOrder from './src/Screens/ViewOrder';

import "firebase/firestore"
import firebase from "firebase"
import firestore from "@react-native-firebase/firestore"

//this is where firebase is initializedd the google.services json is also in the relevant file

const firebaseConfig = 
  apiKey: "AIza<<redacted>>",
  authDomain: "<<redacted>>.firebaseapp.com",
  databaseURL: "https://<<redacted>>.firebaseio.com",
  projectId: "<<redacted>>",
  storageBucket: "<<redacted>>.appspot.com",
  messagingSenderId: "<<redacted>>",
  appId: "1:8<<redacted>>",
  measurementId: "G-T<<redacted>>"
;

// Instantiate a Firebase app.
const firebaseApp = firebase.initializeApp(firebaseConfig);
const db = firebaseApp.firestore()



 const TabNavigator = createBottomTabNavigator(
         
           Home: 
             screen: Home,
             navigationOptions: 
               TabBarIcon: ( tintColor) => <Ionicons name="ios-home" size=24 color=tintColor/>
             
          ,
          // Menus: 
          //  screen: Menus,
          //   navigationOptions: 
          //     TabBarIcon: ( tintColor) => <Ionicons name="ios-chatbox" size=24 color=tintColor/>
          //   
          
           ViewOrder: 
           screen: ViewOrder,
           navigationOptions: 
            TabBarIcon: ( tintColor) => <Ionicons name="ios-add-circle" size=24 color=tintColor/>
            ,
         
       ,
       
           tabBarOptions: 
             activeTintColor: "#161F3D",
             inactiveTintColor: "#E9446A",
             showLabel: true
      
      
           
        
        
       );
      
    
    
     const authStack = createStackNavigator(
       
       Home: Home,
       Login: Login,
       Register: Register,
       // Menu: Menus,
      //  Jitters: Jitters,
      //  Scullery: Scullery,
      //  TheBunker: Bunker,
       // MyAccount: MyAccount,
      //  OrderDetails: OrderDetails,
       ViewOrder: ViewOrder
     );
    const App = createAppContainer(TabNavigator);
     export default createAppContainer(
      (
        createSwitchNavigator(
          
        Loading: LoadingScreen,
        App: TabNavigator,
        Auth: authStack
        ,
        
          initalRouteName: "Loading Page"
        
      )
     )
     );

##Screen to call a collection of firestore##
//this is a console log to see if firestore will initialize 



import React, useState from 'react'
import View, Text from 'react-native'                                                                                                          
import firebase from "firebase"
import firestore from "@react-native-firebase/firestore"



    
async function myFunction() 
   
    var user = firebase.auth().currentUser;
    
    let x = await firestore().collection("Users").doc("MmjyeGlhFPu0g6pK0GQ2").get();

   // console.log(x)





export default class ViewOrder extends React.Component 
    
      componentDidMount()  
        myFunction(
        ) 
         

render() 
    return(
            <View>
                <Text>Hello</Text>
            </View>
   
    )


**//error reads on android emulator**  

as possible unhandled promise rejection (id:0) 
error: no firebase app ['default] has been created - call
firebase.initalize.app() getapp@http://10.0.2.2.8081/index.bundle
?platform 

【问题讨论】:

【参考方案1】:

似乎是错误,主要针对以下部分:

错误:没有创建 Firebase 应用“[DEFAULT]” - 调用 firebase.initializeApp()

您尚未初始化 Firebase 数据库。如文档firebase. app. App 中所述,您需要调用firebase.initializeApp() 来创建应用程序。除此之外,我会将您的导入更改为 import * as firebase from "firebase",因此您正在导入 Firebase 所需的所有包。

我已经查看了来自社区的以下所有帖子,我为您提供的这些可能的解决方案只是可能解决您的问题的一些选项和替代方案。

我建议您查看它们,以便您可以获取更多信息并进一步检查,因为可能有一些其他结构和环境配置可能会影响您,这些案例可以帮助您。

No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() Firebase: No Firebase App '[DEFAULT]' has been created (Firestore) Error: Firebase: No Firebase App '[DEFAULT]' has been created Error: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp()

如果这些信息对您有帮助,请告诉我!

【讨论】:

感谢您回来,我在这个问题上花了一些时间,但无法继续前进!我已经在 app.js 中初始化了 firebase import "firebase/firestore" import * as firebase from "firebase" import firestore from "@react-native-firebase/firestore" const firebaseapp = firebase.initializeApp ( apiKey: , authDomain: databaseURL : , projectId: , storageBucket: , messingsSenderId: , appId: , measureId: ); const db = firebaseapp.firestore() 嗨@norb,这应该可以解决您的问题,这确实使这种情况非常奇怪。考虑到这一点,这可能是您的项目特有的问题,需要 Firebase 团队进行检查。我建议您联系Firebase support。您应该可以直接联系for free。 我已经尝试了很多修复并遵循了有关该问题的所有堆栈但似乎没有任何效果,我已将此转发给 firebase 希望能找到某种解决方案 请告诉我 Firebase 团队是否为您提供帮助以及您是如何修复的,以防您认为答案对您有所帮助。 仍然无法解决这个问题,firebase 告诉我联系 react 8-)

以上是关于设置firebase时出错,可能会出现未处理的承诺拒绝的主要内容,如果未能解决你的问题,请参考以下文章

未处理的异常:[firebase_auth/unknown] null:使用颤振/firebase 进行电话身份验证时出错

如何修复可能的未处理承诺拒绝(id:0)?以及如何修复无法读取未定义的属性“导航”?

解决一堆承诺时出现 Firebase 错误:无法将未定义或 null 转换为对象

使用 axios 时可能出现未处理的 Promise 拒绝、网络错误

捕获存在时可能未处理的承诺拒绝

Firebase 函数 tslint 错误 必须正确处理承诺