Dav*_*ens 4 javascript firebase react-native firebase-authentication
我正在尝试使用firebase身份验证/数据库为react-native移动应用程序创建登录和注册过程.
我可以使用Login.js中的预先存在的电子邮件/密码成功登录.
当我尝试在SignUp.js中创建一个新帐户时,我的问题出现了
用户已正确添加到firebase身份验证,但我希望我的用户配置文件有更多信息.这就是为什么我用他们的名字,电子邮件和我从Signup.js第33行的代码中删除的其他信息写入数据库的原因,第52行有辅助函数.
我的错误undefined is not an object(evaluating this.state.email)
来自SignUp.js中的第33行
这对我来说没有意义,因为我可以在SignUp.js的第27行成功创建用户 this.state.email
是this.state.email
走出去的范围有多大?任何帮助是极大的赞赏.
App.js
import React from 'react';
import { StackNavigator } from 'react-navigation';
import * as firebase from 'firebase';
import Dashboard from './components/Dashboard';
import Login from './components/Login';
import SignUp from './components/SignUp';
const Application = StackNavigator({
Login: { screen: Login},
SignUp: { screen: SignUp},
Dashboard: { screen: Dashboard },
});
export default class App extends React.Component {
componentWillMount(){
const firebaseConfig = {
apiKey: 'MY KEY',
authDomain: 'MY DOMAIN',
databaseURL: 'MY DATABASE URL',
}
if (!firebase.apps.length) {
firebase.initializeApp(firebaseConfig);
}
}
render() {
return (
<Application/>
)}}
Run Code Online (Sandbox Code Playgroud)
Login.js
import React from 'react';
import { StyleSheet, Text, View, KeyboardAvoidingView, ImageBackground } from 'react-native';
import { StackNavigator } from 'react-navigation';
import { Button } from 'react-native-elements';
import { Input } from './Input';
import Dashboard from './Dashboard';
import * as firebase from 'firebase';
export default class Login extends React.Component {
constructor(props){
super(props)
this.state = {
email: '',
password: '',
}
}
loginUser = (email, password, navigate) => {
try{
firebase.auth().signInWithEmailAndPassword(email, password)
.then(function(user){
console.log(user);
navigate('Dashboard', {email, password});
})
}
catch (error){
alert('No known user for that email and password combination')
console.log(error.toString());
}
}
static navigationOptions = { header: null }
render() {
const{ navigate } = this.props.navigation;
return (
<KeyboardAvoidingView
behavior='padding'>
<Input
placeholder = 'Email'
onChangeText = {email => this.setState({email})}
value = {this.state.email}/>
<Input
placeholder = 'Password'
secureTextEntry
onChangeText = {password => this.setState({password})}
value = {this.state.password}/>
<Button
title = 'Log in'
onPress = {() => this.loginUser(this.state.email, this.state.password, navigate)}/>
<Button
title = 'Sign up'
onPress = {() => navigate('SignUp')}/>
</KeyboardAvoidingView>
);
}
}
Run Code Online (Sandbox Code Playgroud)
SignUp.js
import React from 'react';
import { StyleSheet, Text, View, KeyboardAvoidingView, ImageBackground } from 'react-native';
import { StackNavigator } from 'react-navigation';
import { Button } from 'react-native-elements';
import { Input } from './Input';
import Dashboard from './Dashboard';
import * as firebase from 'firebase';
export default class SignUp extends React.Component {
constructor(props){
super(props)
this.state = {
firstName: '',
lastName: '',
email: '',
password: '',
confirmPassword: '',
}
}
signUpUser = () => {
try{
if(this.state.password === this.state.confirmPassword){
console.log('CREATING USER...');
firebase.auth().createUserWithEmailAndPassword(this.state.email, this.state.password).then( response => {
console.log('SIGNING IN...');
firebase.auth().signInWithEmailAndPassword(this.state.email, this.state.password)
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
console.log('WRITING TO DATABASE...');
this.writeUserData(user, this.state.email, this.state.firstName, this.state.lastName);
}
else {
alert('Something went wrong. Please try again.');
return;
}
});
})
}
else{
alert('Passwords do not match');
return;
}
}
catch(error){
console.log(error.toString());
}
}
writeUserData = (user, email, first, last) => {
console.log('ADDING USER ' + user.uid)
try{
firebase.database().ref('users/' + user.uid).set({
email: email,
first: first,
last: last,
});
}
catch(error){
console.log(error.toString());
}
console.log('WRITE COMPLETE')
//navigate to dashboard
}
static navigationOptions = { header: null }
render() {
const{ navigate } = this.props.navigation;
return (
<KeyboardAvoidingView
behavior='padding'>
<Input
placeholder = 'First Name'
onChangeText = {firstName => this.setState({firstName})}
value = {this.state.firstName}/>
<Input
placeholder = 'Last Name'
onChangeText = {lastName => this.setState({lastName})}
value = {this.state.lastName}/>
<Input
placeholder = 'Email'
onChangeText = {email => this.setState({email})}
value = {this.state.email}/>
<Input
placeholder = 'Password'
secureTextEntry
onChangeText = {password => this.setState({password})}
value = {this.state.password}/>
<Input
placeholder = 'Confirm password'
secureTextEntry
onChangeText = {confirmPassword => this.setState({confirmPassword})}
value = {this.state.confirmPassword}/>
<Button
title = 'Create Account'
onPress = {() => this.signUpUser()}/>
<Text
activeOpacity={0.75}
onPress = {() => this.props.navigation.goBack()}>
Go back
</Text>
</KeyboardAvoidingView>
);
}
}
Run Code Online (Sandbox Code Playgroud)
Aur*_*ílý 10
this
当它处于不同的功能时改变意义.这基本上是对象方法在Javascript中工作的原因,因为它this
总是引用包含它的类.当您this
在匿名函数中使用时,就像在SignUp.js
文件中一样,您没有this
像以前那样使用它.
一个简单的解决方法是添加如下行:
let self = this;
Run Code Online (Sandbox Code Playgroud)
或者,如果您只需要州:
let state = this.state;
Run Code Online (Sandbox Code Playgroud)
之前的firebase.auth().onAuthStateChanged(function(user) {
位.然后在里面,你使用你创建的变量而不是this
.
另一件要尝试的是将您的匿名更改function
为箭头功能.箭头函数不会修改this
变量.所以,像:
firebase.auth().onAuthStateChanged((user) => {
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1543 次 |
最近记录: |