简体   繁体   中英

firebase.database is not a function why I am getting this error in react native app?

I am getting this error when I tried to fetch data from Firebase realtime database-

AboutApp.js:9 Uncaught TypeError: config__WEBPACK_IMPORTED_MODULE_9 _.firebase.database is not a function at AboutApp.js:9:1

Is there anything wrong on my code?

Please check the database structure

在此处输入图像描述

import React, { useState, useEffect } from 'react';
import {View, Button, Text, FlatList, StyleSheet, Pressable, TouchableOpacity} from 'react-native'
import {firebase} from '../config';


const AboutApp = ({ navigation }) =>{
const [users, setUsers] = useState([]);
useEffect(() => {
 const users = firebase.database().ref("first");
 users.on("value",datasnap=>{
    console.log(datasnap.val());
 });
 setUsers(users)

}, [])


    return (
       <View style={{ flex:1, marginTop: 100}}>
           <FlatList
           style={{height: '100%'}}
           data={users}
           numColumns={1}
           renderItem={({item}) => (
             <Pressable
         style={styles.container}>
         <View style={styles.innerContainer}>
             <Text>{item.name}</Text>
             <Text>{item.email}</Text>
             <Text>{item.phone}</Text>
             
         </View>
         
             </Pressable>
           )}
           />
            </View>
         );
         
         }
    export default AboutApp; 

Please check the config file-

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


const firebaseConfig = {
  apiKey: "AIzaSyBS93aTCkwMyEWU15HR5y454qZM_WTDXhAo",
  authDomain: "newawesome-18ee9.firebaseapp.com",
  projectId: "newawesome-18ee9",
  storageBucket: "newawesome-18ee9.appspot.com",
  messagingSenderId: "441185679151",
  appId: "1:441187374121:web:ecf6de832274a4480b8610",
  measurementId: "G-R6NM36814P"
};

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

It seems you are trying to use Firebase Realtime Database and not Firestore . You are not importing the RTDB SDK. Try adding the following import:

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

import 'firebase/compat/database' // <-- Import RTDB SDK

Also, Firebase will be removing the "compat" SDK in future updates so it'll be best to upgrade your code to the new Modular SDK. Checkout the documentation for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM