开发者问题收集

可能未处理的承诺拒绝反应本机错误

2022-11-21
504

我正在制作一个 react-native 用户注册和登录应用程序,并收到以下错误。我该如何解决这个问题?我的代码存储的数据与我用于注册的数据完全相同。在登录页面中,它拒绝登录并显示(登录失败)错误。在控制台中,它打印以下错误。请帮忙。

警告:可能未处理的承诺拒绝 TypeError:未定义不是对象(评估'UserData.password')

我的代码

import React, { useState } from "react";
import {StyleSheet, View} from 'react-native';
import {Input, Button, Card, Tab} from '@rneui/themed';
import { FontAwesome } from '@expo/vector-icons';
import {AuthContext} from "../topuproviders/Authprovider";
import { getDataJSON } from "../functions/topuAsyncStorage";


const SignInScreen = (props) => {
const [Email, setEmail]         = useState("");
const [Password, setPassword]   = useState(""); 
 return (
     <AuthContext.Consumer>
       {(auth)=>(<View style ={styles.viewStyle}>
         <Card>
            <Card.Title>Welcome to Auth App!</Card.Title>
            <Card.Divider/>
                <Input 
                    placeholder='Email Address' 
                    leftIcon={<FontAwesome name="envelope" size={20} color="#4CAF50" />}
                    onChangeText={function(currentInput){
                        setEmail(currentInput)
                    }}
                />

                <Input 
                    placeholder='Password' 
                    secureTextEntry={true} 
                    leftIcon={<FontAwesome name="lock" size={24} color="#4CAF50" />}
                    onChangeText={function(currentInput){
                        setPassword(currentInput)
                    }}
                />

                <Button 
                    icon = {<FontAwesome name="arrow-circle-o-right" size={15} color="white" />}  
                    name ="login" 
                    type ="solid" 
                    title="Sign In" 
                    onPress={
                        async function (){
                           let UserData = await getDataJSON(Email);
                           if(UserData.password == Password){
                            auth.SetIsLoggedIn(true);
                            auth.setCurrentUser(UserData);
                           }else{
                            alert('Login failed');
                            console.log(UserData); /*** Prints exactly same data what i Input ***/
                           }
                        }
                    }
                />
            <Card.Divider/>
            
            <Button 
                name ="noaccount" 
                type ="clear" 
                title="Don't have account?"
                onPress={
                    function (){
                        props.navigation.navigate("SignUp")
                    }
                }
            />
         </Card>
         
     </View>)}
     </AuthContext.Consumer>    

   );
 }

 export default SignInScreen;

getdataJson/Asyncstorage

import AsyncStorage from '@react-native-async-storage/async-storage';

const storeData = async (key, value) => {
try {
    await AsyncStorage.setItem(key, value);
    alert("Data stored succefully");
}catch (error){
    alert(error);
 }
};

const storeDataJson = async (key, value) => {
try {
    const jsonValue = JSON.stringify(value);
    await AsyncStorage.setItem(key, jsonValue);
    alert("Data stored successfully")
 }catch(error){
    alert(error);
 }
};

const getData = async (key) => {
 try {
    let data = await AsyncStorage.getItem(key);
    if(data != null){
        return data;
    }else{
        alert("No Data with this key!");
    }
  }catch (error){
    alert(error);
  }
};

const getDataJSON = async (key) => {
try {
    let data = await AsyncStorage.getItem(key);
    if(data != null){
        const jsonData = JSON.parse(data);
        return jsonData;
    }else {
        alert("No data with this key");
    }
 }catch(error){
    alert(error);
    
 }
};

const removeData = async (key) => {
try {
    await AsyncStorage.removeItem(key);
    alert("Data removed Successfully");
 }catch (error){
    alert (error);
 }
};

export {storeData, storeDataJson, getData, getDataJSON, removeData}
2个回答

您可以将函数包装在 try-catch 块内以查看错误。

async function (){
         try {

          let UserData = await getDataJSON(Email);
          if(UserData.password == Password){
             auth.SetIsLoggedIn(true);
             auth.setCurrentUser(UserData);
          }else{
              alert('Login failed');
              console.log(UserData);
          }
   }
   catch(error){
     console.log(error)
   }
}

getDataJson

const getDataJSON = (key) => {
 return await AsyncStorage.getItem(key);
};

此外,据我所知,您无法在 react-native 中发出警报。您需要从“react-native”导入 alert

官方文档

从“react-native”导入 { Alert };

Ferin Patel
2022-11-24

您的 getDataJSON() 函数并不总是返回对象。因此,您应该首先检查其值是否为 true、是否为对象,以及是否具有“密码”属性。为此,请使用问号访问密码属性。

let UserData = await getDataJSON(Email);
if(UserData?.password == Password){
...
}
Hossein Arsheia
2022-11-24