這個錯誤很奇怪。在 firebase 中 react native 有什么問題我已經通過 npm install 安裝了 firebase,這里是我的代碼
import React, {Component} from 'react'
import {View} from 'react-native'
import Card from './card'
import { initializeApp } from 'firebase/app'
import firebase from 'firebase/compat/app'
import database from 'firebase/database'
import 'firebase/database'
const firebaseConfig = {
apiKey: "AIzaSyA_AXaJ_hLC3jXMOIjaXLyktgX1t7e--FU",
authDomain: "tinderclone-99d8e.firebaseapp.com",
projectId: "tinderclone-99d8e",
storageBucket: "tinderclone-99d8e.appspot.com",
messagingSenderId: "264605926507",
appId: "1:264605926507:web:c9309a6669322ef6839d2e",
databaseURL:'https://tinderclone-99d8e-default-rtdb.asia-southeast1.firebasedatabase.app/'
};
firebase.initializeApp(firebaseConfig)
export default class App extends Component {
state = {
profileIndex: 0,
}
componentWillMount() {
firebase.database().ref().child('users').once('value', (snap) => {
console.log('Data', snap.val())
})
}
nextCard = () => {
this.setState({profileIndex: this.state.profileIndex 1})
}
render() {
const {profileIndex} = this.state
return (
<View style={{flex:1}}>
{profiles.slice(profileIndex, profileIndex 3).reverse().map((profile) => {
return (
<Card
key={profile.id}
profile={profile}
onSwipeOff={this.nextCard}
/>
)
})}
</View>
)
}
}
const profiles = [
{
id: '259389830744794',
name: 'Candice',
birthday: '10/18/1986',
bio: 'Supermodel',
},
{
id: '720115413',
name: 'Alessandra',
birthday: '1/10/1989',
bio: 'Dancer',
},
{
id: '169571172540',
name: 'Miranda',
birthday: '12/12/1983',
bio: 'Doctor',
},
{
id: '1476279359358140',
name: 'Alissa',
birthday: '2/11/1990',
bio: 'Comedian',
},
{
id: '1140849052644433',
name: 'Behati',
birthday: '3/23/1991',
bio: 'Developer',
},
{
id: '912478262117011',
name: 'Rosie',
birthday: '9/4/1989',
bio: 'Artist',
},
{
id: '173567062703796',
name: 'Kendall',
birthday: '8/17/1992',
bio: 'Truck Driver',
},
]
錯誤是
TypeError: _app2.default.database is not a function. (In '_app2.default.database()', '_app2.default.database' is undefined)
This error is located at:
in App (created by ExpoRoot)
in ExpoRoot
in RCTView (created by View)
in View (created by AppContainer)
in RCTView (created by View)
in View (created by AppContainer)
in AppContainer
如何解決這個錯誤我嘗試連接實時資料庫

有人可以幫我解決這個問題代碼有什么問題我想知道為什么很難與 firebase 連接。這個代碼資料庫連接中的嚴重錯誤在 firebase 中并不簡單。真的很驚訝。
uj5u.com熱心網友回復:
您正在匯入 Firebase v9(據我所知):
import database from 'firebase/database'
此匯入意味著您正在使用 v9 SDK 的新模塊化語法。
但隨后在您的代碼中,您嘗試使用以下方式訪問資料庫:
firebase.database()...
這是您未匯入的 v8 樣式語法。
如果你想繼續使用V8引擎的語法,你應該匯入compat像你一樣的庫import firebase from 'firebase/compat/app'。所以:
import database from 'firebase/compat/database'
另請參閱 Firebase v9 升級指南。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/347736.html
標籤:javascript 火力基地 Firebase 实时数据库
