【问题标题】:TypeError: null is not an object (evaluating 'user.uid') React Native FireBaseTypeError:null 不是对象(评估'user.uid')React Native FireBase
【发布时间】:2022-01-15 20:23:24
【问题描述】:

我从 AuthContext 中提取的 user.uid 和 useContext 给出了这样的错误。 基本上,如果用户 ID 等于已登录,我会让共享帖子删除。 但是 user.uid == post.userId 这不起作用并且是错误的:TypeError: null is not an object (evalating 'user.uid') 如何解决?

import { AuthContext } from "../../Pages/AuthProvider/AuthProvider";
const PostCard = ({post}) => {
    const {user} = useContext(AuthContext); 
    const userID=user.uid;
    return(
        <View style={styles.container}>
            <View style={styles.header}>
                <View style={styles.inner1_container}>
                    <Image
                        style={styles.profile_image}
                        source={ProfileImg}
                    />
                    <Text style={styles.username_text}>username</Text>
                </View>
                <View style={styles.inner2_container}>
                    <Text style={styles.like_text}>Patileyenler: {post.likes}</Text>
                    <View style={styles.like_button}>
                    <TouchableWithoutFeedback
                        onPress={liked}
                    >
                        <Image
                            style={styles.like_button_image}
                            source={isLiked ? likedPaw : unLikedPaw}
                        />
                    </TouchableWithoutFeedback>
                    </View>
                </View>                
            </View>

            <View style={styles.describe_container}>
                <Text numberOfLines={8} style={styles.describe_text}>{post.descreption}</Text>
                {
                    post.contact != "" && (<>
                        <Text numberOfLines={2} style={styles.contact_text}>{iltetisimtext}{post.contact} </Text>
                    </>) 
                }
                <Text style={styles.time_text}>Paylaşma Zamanı</Text>
                {
                    user.uid == post.userId && (<>
                    <Text style={styles.time_text}>Delete</Text>
                    </>)
                }
            </View>
        </View>        
    )
}

【问题讨论】:

    标签: javascript android ios firebase react-native


    【解决方案1】:

    错误的原因是我们注销时用户对象为空。我用 if 进行了检查并解决了错误。

        function deletePostText(){
            if(user){
                if(user.uid == post.userId){
                     return(
                        <TouchableOpacity>
                            <Text style={styles.time_text}>Delete</Text>
                        </TouchableOpacity>
                    )
                }
               }
            else{
                return;
            }
        }  

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      • 2021-06-22
      • 2020-04-15
      • 1970-01-01
      • 2021-01-13
      • 2020-11-21
      • 2021-05-27
      相关资源
      最近更新 更多