我已经安装了最新版本的react-native和firebase。当我尝试调用“ firebase.auth()。signInAndRetrieveDataWithEmailAndPassword(email,password)”时,出现错误,并说“ _auth.default.auth不是函数”
根文件夹文件中的没有得到这种错误。它的工作。但是我进入了文件夹文件并调用了fire base函数,将发生此错误。
根文件夹文件App.js
“ firebase.auth()。signInAndRetrieveDataWithEmailAndPassword(电子邮件,密码)”
工作中..
./ src / component / LoginForm.js
“ firebase.auth()。signInAndRetrieveDataWithEmailAndPassword(电子邮件,密码)”
不起作用。克服错误。
------------------ App.js根级别(带有“ firebase.auth().onAuthStateChanged((user)
”的工作代码)------------ -------
import React, { Component } from 'react';
import { View } from 'react-native';
import { Header, Button, Spinner } from './components/common';
import LoginForm from './components/LoginForm';
import firebase from '@firebase/app';
class App extends Component {
state = { loggedIn: false };
componentWillMount() {
var config = {
apiKey: 'xxxxxxxxxxxxxxx-MY',
authDomain: 'xxxx-a115e.firebaseapp.com',
databaseURL: 'https://xxxx-xxxx.firebaseio.com',
projectId: 'xxx-xx',
storageBucket: 'xxxx-a115e.appspot.com',
messagingSenderId: 'xxxxxxxxx'
};
firebase.initializeApp(config);
firebase.auth().onAuthStateChanged((user) => {
if(user) {
this.setState({ loggedIn: true });
}else {
this.setState({ loggedIn: false });
}
});
}
------------- / src / components / LoginForm.js文件不起作用(firebase.auth().signInAndRetrieveDataWithEmailAndPassword
)--------------- < / p>
import React, { Component } from 'react';
import { firebase } from '@firebase/auth';
import { Text } from 'react-native';
import { Button, Card, CardSection, Input, Spinner } from './common';
class LoginForm extends Component {
state = { email: '', password: '', error: '', loading: false };
onButtonPress() {
const { email, password } = this.state;
//var user = firebase.auth().currentUser;
this.setState({ error: '', loading: true});
firebase.auth().signInAndRetrieveDataWithEmailAndPassword(email, password)
.then(this.onLoginSucess.bind(this))
.catch( () => {
firebase.auth().createUserAndRetrieveDataWithEmailAndPassword(email, password)
.then(this.onLoginSucess.bind(this))
.catch(this.onLoginFail.bind(this));
});
}
我确定发生了这种情况,因为文件不在项目文件结构的根目录下。帮我解决这个问题。
答案 0 :(得分:0)
新的firebase API必须使用此类导入进行身份验证。
import firebase from 'firebase/app';
import 'firebase/auth';
我正在使用Firebase版本5.11.0。希望这会有所帮助:)