【问题标题】:null is not an object (evaluating 'str.split') React Nativenull 不是一个对象(评估'str.split')React Native
【发布时间】:2021-10-27 01:08:02
【问题描述】:

在我的应用上注册用户时,我不断收到错误“null is not an object (evalating 'str.split')”。注册后,用户登陆 HomeScreen,其中包含“str.split”字符串,该字符串将 firebase 显示名称拆分为第一个单词(名字)。无法弄清楚如何修复错误。

import { View, Text, Image, Button, StyleSheet } from 'react-native';
import { auth } from '../firebase';

const HomeScreen = ({ navigation }) => {

    var str = auth?.currentUser?.displayName;
    const welcomeText = (str.split(' ')[0]);
    
    return (
        <View style={styles.homeContainer}>
                <View style={styles.overviewContainer}>
                    <View style={styles.welcomeContainer}>
                        <Text style={styles.welcomeText}>Hello <Text style={styles.welcomeTextBold}>{welcomeText}</Text></Text>
                        <Image style={styles.welcomeIcon} source={require('../assets/images/Welcome-Icon.png')} />
                    </View> 
                </View>
        </View>

【问题讨论】:

  • 错误是str为空。所以authauth.currentUserauth.currentUser.displayName都没有设置。
  • console.log str 并检查它是否是有效的string
  • @Rohit 它是一个有效的字符串,它工作得很好,它拆分了 displayName 但我仍然收到错误
  • 你确定它是一个完美的字符串,即使是新注册的用户?默认显示名称为空。\

标签: javascript firebase react-native jsx


【解决方案1】:

更改下一行

const welcomeText = (str.split(' ')[0]);

const welcomeText = (str?.split(' ')[0]) || '';

【讨论】:

    猜你喜欢
    • 2017-09-19
    • 1970-01-01
    • 2021-08-30
    • 2018-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多