【发布时间】:2019-02-14 03:35:36
【问题描述】:
我已经安装了最新版本的 react-native 和 firebase。当我试图调用“firebase.auth().signInAndRetrieveDataWithEmailAndPassword(email, password)”时,它会出现错误并说“_auth.default.auth 不是函数”
在根文件夹文件中没有出现这种错误。它的工作。但我进入文件夹文件并调用火基函数会发生此错误。
根文件夹文件 App.js
“firebase.auth().signInAndRetrieveDataWithEmailAndPassword(email, password)”
工作..
./src/component/LoginForm.js
“firebase.auth().signInAndRetrieveDataWithEmailAndPassword(email, password)”
不工作。出现上述错误。
------------------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)---------------
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));
});
}
我发现这是因为文件不在项目文件结构的根级别中。帮我解决这个问题..
【问题讨论】:
-
您为 Firebase 使用了哪个库?
-
npm install firebase --save 和 npm install firebase-admin --save 我都安装了..当前可用版本..最新
-
这发生在 Android 模拟器上..
标签: firebase react-native visual-studio-code react-native-android