首页 > 解决方案 > TypeError:null 不是对象(评估 '_firebase.default.auth().currentUser.displayName')

问题描述

**请帮助任何人。我无法退出 Firebase。它不断给我一个错误。下面是我的代码。** 我已经尝试过当前用户的认证,但是还是说当前用户对象为空,一个用户是一个对象。而且我已经在仪表板类中调用了这个函数,但是如果我单独调用这个 LoginOut 组件,它就可以正常工作。可能是dashboard类中的调用组件有问题,我一头雾水,救命啊?!!

// database/firebaseDb.js

import * as firebase from 'firebase';

 const firebaseConfig = {
    apiKey: "AIzaSyADgtLqQHs1KAlnMKUn8uM_vpasRUkWchw",
    authDomain: "reactnativefirebase-9ed38.firebaseapp.com",
    projectId: "reactnativefirebase-9ed38",
    storageBucket: "reactnativefirebase-9ed38.appspot.com",
    messagingSenderId: "117218702170",
    appId: "1:117218702170:web:94a6ad8eea017273c43963",
    measurementId: "G-2RN9L587XJ"
  };
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);

export default firebase;


import React, { Component } from 'react';
import { StyleSheet, View, Text, Button } from 'react-native';
import firebase from '../database/firebase';

export default class LoginOut extends Component {
  constructor() {
    super();
    this.state = { 
      uid: ''
    }
  }

  signOut = () => {
    firebase.auth().signOut().then(() => {
      this.props.navigation.navigate('Login')
    })
    .catch(error => this.setState({ errorMessage: error.message }))
  }  

  render() {
    
    this.state = { 
      displayName: firebase.auth().currentUser.displayName, ////Firebase User Authentification /////
      uid: firebase.auth().currentUser.uid
    }
    
    return (
      <View style={styles.container}>
        <Text style = {styles.textStyle}>
          Hello, {firebase.auth().currentUser.displayName}
        </Text>

        <Button
          color="tomato"
          title="Log out"
          onPress={() => this.signOut()} ///Calling Sign Out Function /////
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    display: "flex",
    justifyContent: 'center',
    alignItems: 'center',
    padding: 35,
    backgroundColor: 'wheat'
  },
  textStyle: {
    fontSize: 15,
    marginBottom: 20
  }
});

标签: react-nativefirebase-realtime-databasefirebase-authenticationexposingle-logout

解决方案


推荐阅读