当我第一次单击登录按钮时,我获得的价值是“未定义的”,第二次单击后,它将显示确切的价值,我所除外。 我的要求是,我需要检查有效的用户名和密码,如果正确,则获取特定用户的详细信息,一旦检索到数据,我需要浏览另一页, 下面是代码,请帮助调用:
import PropTypes from "prop-types";
import React, { Component } from "react";
import {
View,
Text,
Button,
TouchableOpacity,
TextInput,
StyleSheet,
NavigationActions
} from "react-native";
import firebase from "firebase";
export class Login extends Component {
state = {
email: "xxxxxx",
password: "123456",
items: [],
userData: [],
currentUser: null,
role: ""
};
login = (email, pass) => {
this.validateFirebase(email, pass);
};
validateFirebase = (email, pass) => {
firebase
.auth()
.signInWithEmailAndPassword("xxxxxx.com", "123456")
.then(() => {
{this.forgotPasswordForm()}
})
.catch(function(error) {
console.error("Error: ", error);
});
};
// Create a function that will update the state in parent
forgotPasswordForm = () => {
const { currentUser } = firebase.auth();
let u_ref = currentUser.uid;
let userEmail = currentUser.email;
firebase.database()
.ref("users_ids/0")
.once("value")
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var key = childSnapshot.key;
var childData = childSnapshot.val();
if (key == u_ref) {
hello = childData.role;
if(hello == 'employee'){
this.props.navigation.navigate("driverloginScreen");
}
}
});
});
}
render() {
return (
<View style={styles.container}>
<TextInput
style={styles.input}
underlineColorAndroid="transparent"
placeholder="Email"
placeholderTextColor="black"
autoCapitalize="none"
onChangeText={this.handleEmail}
/>
<TextInput
style={styles.input}
underlineColorAndroid="transparent"
placeholder="Password"
placeholderTextColor="black"
autoCapitalize="none"
onChangeText={this.handlePassword}
/>
<TouchableOpacity
style={styles.submitButton}
onPress={() => this.login(this.state.email, this.state.password)}
>
<Text style={styles.submitButtonText}> Submit </Text>
</TouchableOpacity>
</View>
);
}
}
export default Login;