这个错误很奇怪。在 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 中的数据库连接并不简单。真的很惊喜。
最佳答案
您正在导入 Firebase v9(据我所知):
import database from 'firebase/database'
此导入意味着您正在使用 v9 SDK 的新模块化语法。
但随后在您的代码中您尝试使用以下方式访问数据库:
firebase.database()...
这是您没有导入的 v8 样式语法。
如果你想继续使用 v8 语法,你应该像 import firebase from 'firebase/compat/app'
一样导入 compat
库。所以:
import database from 'firebase/compat/database'
另请参阅 Firebase v9 upgrade guide .
关于javascript - TypeError : _app2. default.database 不是函数。 (在 '_app2.default.database()' 中, '_app2.default.database' 未定义),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69839553/