我已经在项目中安装并导入了Firebase,代码如下:
componentWillMount(){
var config = {
apiKey: "***************************",
authDomain: "configuracaofirebase-3a6ef.firebaseapp.com",
databaseURL: "https://configuracaofirebase-3a6ef.firebaseio.com",
projectId: "configuracaofirebase-3a6ef",
storageBucket: "configuracaofirebase-3a6ef.appspot.com",
messagingSenderId: "939162871117"
};
firebase.initializeApp(config);
}
salvarDados(){
var database = firebase.database();
database.ref("pontuacao").set("100");
}
render() {
return (
<View style={styles.container}>
<Button
onPress={ () => { this.salvarDados() } }
title="Salvar dados"
color="#841584"
accessibilityLabel="Salvar dados"
/>
<Text>Meu App</Text>
</View>
);
}
当我调用“ salvarDados”方法时不起作用
显示此错误:
typeError: undifined is not a function (evaluating '_app.default.database()')
答案 0 :(得分:1)
我通过添加一个新的导入来解决了这个问题:
import firebase from '@firebase/app';
import '@firebase/database';
答案 1 :(得分:0)
尝试删除函数调用周围的{}:
onPress={ () => { this.salvarDados() }
将成为:
onPress={ () => this.salvarDados() }