我正在尝试在我的本机应用程序中初始化我的Firebase。当我使用Firebase.init()从Firebase.js调用函数init()到App.js时,将出现错误消息。
我已排除了代码中不重要的部分。
//App.js
import Firebase from './database/Firebase';
import {
createStackNavigator,
createAppContainer,
} from 'react-navigation';
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
authStatusReported: false,
isUserAuthenticated: false,
}
}
componentWillMount() {
Firebase.init();
Firebase.auth.onAuthStateChanged(user => {
this.setState({
authStatusReported: true,
isUserAuthenticated: !!user,
})
});
}
render() {
return (
<AppNavigator/>
);
}
}
//Firebase.js
import React, { Component } from 'react';
import * as firebase from "firebase";
import "firebase/firestore";
//const Firebase = require('firebase');
const firebaseConfig = {
apiKey: "AIzaSyA-3vaV9v0Yz2sKObtCJw6ErRaXrU7e9H8",
authDomain: "travelgo-248207.firebaseapp.com",
databaseURL: "https://travelgo-248207.firebaseio.com",
projectId: "trvelgo-248207",
storageBucket: "travelgo-248207.appspot.com",
messagingSenderId: "915334841704",
appId: "1:915334841704:android:345a8604f2dc7ea7"
};
export class Firebase extends Component {
static auth;
static registrationInfo = {
name: '',
email: '',
}
static writeUserData(email, name) {
//get the current user
var user = firebase.auth().currentUser
//enter the users data into the database
firebase.database().ref('Users/' + user.uid).set({
name: name,
email: email,
}).then((data) => {
//success callback
console.log('data', data)
alert('Account is created!');
}).catch((error) => {
//error callback
console.log('error', error);
alert(error);
})
}
static readUserData() {
var user = firebase.auth().currentUser
firebase.database().ref('Users/' + user.uid).once('value', function (snapshot) {
console.log(snapshot.val())
});
}
static updateEmail(email) {
var user = firebase.auth().currentUser
firebase.database().ref('Users/'+ user.uid).update({
email,
});
}
// Initialize Firebase
static init() {
firebase.initializeApp(firebaseConfig);
Firebase.auth = firebase.auth();
Firebase.firestore = firebase.firestore();
Firebase.storage = firebase.storage();
}
}