可能的未处理的承诺拒绝(id:0):ReferenceError:找不到变量TextInputEmail

时间:2020-01-30 07:46:38

标签: react-native react-native-android react-native-ios

enter image description here

import React,{Component} from 'react';
import {View,Text,Image,ImageBackground,
         TextInput,TouchableOpacity,AsyncStorage,
        ActivityIndicator,StatusBar,StyleSheet} from 'react-native';
import {createStackNavigator} from 'react-navigation-stack' ;
import {createAppContainer,createSwitchNavigator} from 'react-navigation';
import AppContainer from '../App'
import Forgot from '../Screens/Forgot' ;
 class Home extends Component {
  constructor(props) {
    super(props);
    this.state = {
      TextInputEmail: '',
      TextInputPassword: '',
    };
  }

   CheckTextInput = async() =>
   {
     if (this.state.TextInputEmail != '')
         {
            if(this.state.TextInputPassword != '')
           {
              // if(userInfo.TextInputEmail === this.state.TextInputEmail &&
            // userInfo.TextInputPassword===this.state.TextInputPassword)

              fetch('http://104.197.28.169:3000/auth/login?', {
                method: 'POST',
                body: JSON.stringify({
                  "email": TextInputEmail,
                  "password": TextInputPassword
                }),
                })
                .then((response) => response.json())
                .then(() => {
                  this.props.navigation.navigate('drawernavi')
                    console.log('response object:',responseJson)
                })
                .catch((error) => {
                  console.error(error);
                  throw error;
                });
              //  await AsyncStorage.setItem('isLoggedIn','1');
              // this.props.navigation.navigate('drawernavi')
              //this.userLogin()
               // alert('Legged in')
              // this.userLogin();
          }
           else
            alert('Please Enter Password');
        } else
        alert('Please Enter Email & Password');
   }



  render(){
    return(

      <View>
       <ImageBackground style={{width:'100%',height:'100%'}}
        source={require('../images/login-screen.png')}>
        <View style={{flex:.8,alignItems:'center',justifyContent:'center',marginTop:20}}>


        <View style={styles.imageView}>
<Image style={styles.logoImg}
source = {require('../images/core-logo.png')}
></Image>
</View>


          <TextInput style={{fontSize:15,borderWidth:1,borderRadius:20,paddingLeft:15,width:300,
          borderColor:'black',height:40,marginTop:40}} 
          keyboardType='email-address'
          maxLength={32}
          placeholder='Email' placeholderTextColor='#a8a8a8'
          onChangeText={TextInputEmail => this.setState({ TextInputEmail })}
          value= {this.state.TextInputEmail}
          underlineColorAndroid="transparent">
          </TextInput>

          <TextInput style={{width:300,fontSize:15,borderWidth:1,borderRadius:20,paddingLeft:15,
          borderColor:'black',marginTop:20,height:40}}
          secureTextEntry={true}
          maxLength={14}

           placeholder='Password' placeholderTextColor='#a8a8a8'
           onChangeText={TextInputPassword => this.setState({ TextInputPassword })}
           value= {this.state.TextInputPassword}
           underlineColorAndroid="transparent">
          </TextInput>

          <TouchableOpacity
          style={{width:300,marginTop:35,paddingTop:10,paddingBottom:10,backgroundColor:'#2F6995',
            borderRadius:20,borderWidth: 1,borderColor: '#fff'}}

            onPress={this.CheckTextInput}
           underlayColor='#fff'>
          <Text style={{color:'#fff',textAlign:'center', paddingLeft : 10, paddingRight : 10,fontSize:17}}>LOGIN</Text>


     </TouchableOpacity>
         <View style={{alignItems:'center',marginTop:30}}>
        <TouchableOpacity
         onPress={() => this.props.navigation.navigate('forgotstack')} >
         <Text style={{fontSize:12.5,color:'black',borderBottomWidth:1}}> Forgot Password ? </Text>
       </TouchableOpacity>
       </View>
       </View>

        </ImageBackground>
      </View>

    );
  }
}

我正在创建登录功能。我在处理它时遇到此错误。当我尝试登录时,黄色警告开始出现 我正在通过node.js api获取数据。因此要求api和react中的输入字段名称相同或可以不同或存在另一种方式,如果错误,请建议更正我的代码

2 个答案:

答案 0 :(得分:0)

问题是在这种情况下if(userInfo.TextInputEmail === this.state.TextInputEmail)尚未定义userInfo,所以未定义userInfo.TextInputEmail。

请检查一次

答案 1 :(得分:0)

     body: JSON.stringify({
                  "email": TextInputEmail,
                  "password": TextInputPassword
                }),
//Change it to
 body: JSON.stringify({
                  "email": this.state.TextInputEmail,
                  "password": this.state.TextInputPassword
                }),