英文:
TypeError: undefined is not an object (evaluating 'label.toString')
问题
我正在使用React Native,当我使用类似这样的JavaScript函数时,出现了错误:
const User = ({ navigation, route }) => {
//SIGN OUT
function signOutHandler() {
signOut(auth)
.then(() => {
console.log('user signed out')
navigation.navigate('SignIn')
})
.catch(err => {
// console.log(err.message)
})
}
let label = route.params.name
label.toString().charAt(0).toUpperCase();
console.log(label)
return (
<View style={styles.userContainer}>
<View style={styles.bio}>
<IconButton name="account-circle" size={70} style={styles.pp} />
<View style={styles.cred}>
<Text style={styles.text}>{label}</Text>
<Text style={styles.text}>{route.params.email}</Text>
<Text style={styles.text}>You have currently <Text style={styles.bold}>{route.params.notes.length}</Text> notes.</Text>
</View>
</View>
<View style={styles.bc}>
<Button title='Delete Account' color={'red'} onPress={signOutHandler} />
<Button title='Sign Out' onPress={signOutHandler} />
</View>
</View>
)
}
export default User
我也无法获取数组的长度(使用.length()),它会抛出相同的错误。我认为我可能犯了一个简单的错误。如果你能帮助我,我将不胜感激。
英文:
I'm using React Native and I got errors when I use JavaScript functions like this:
const User = ({navigation, route}) => {
//SIGN OUT
function signOutHandler() {
signOut(auth)
.then(() => {
console.log('user signed out')
navigation.navigate('SignIn')
})
.catch(err => {
// console.log(err.message)
})
}
let label = route.params.name
label.toString().charAt(0).toUppercase();
console.log(label)
return (
<View style={styles.userContainer}>
<View style={styles.bio}>
<IconButton name="account-circle" size={70} style={styles.pp} />
<View style={styles.cred}>
<Text style={styles.text}>{label}</Text>
<Text style={styles.text}>{route.params.email}</Text>
<Text style={styles.text}>You have currently <Text style={styles.bold}>{route.params.notes.length}</Text> notes.</Text>
</View>
</View>
<View style={styles.bc}>
<Button title='Delete Account' color={'red'} onPress={signOutHandler} />
<Button title='Sign Out' onPress={signOutHandler} />
</View>
</View>
)
}
export default User
I can't get the length (with .length()) of an array also. It throws the same error. I think I'm making a simple mistake. I will be glad if you can help me.
答案1
得分: 2
如果label
的值为undefined
,意味着route.params.name
不存在 - name
不是params
的一个属性。你可以使用可选链接 来防止错误。
在JavaScript中,我们使用简单的length
来获取数组的长度,而不是length()
,因为它是一个属性而不是方法。
英文:
If the value of label
is undefined
it means that route.params.name
is not present - name
is not a prop on params
. You could use optional chaining to prevent an error.
In JavaScript we get the length of arrays using a simple length
instead of length()
since its a prop not a method.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论