TypeError: undefined is not an object (evaluating ‘label.toString’)

huangapple go评论68阅读模式
英文:

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}) =&gt; {
//SIGN OUT
function signOutHandler() {
signOut(auth)
.then(() =&gt; {
console.log(&#39;user signed out&#39;)
navigation.navigate(&#39;SignIn&#39;)
})
.catch(err =&gt; {
// console.log(err.message)
})
}
let label = route.params.name
label.toString().charAt(0).toUppercase();
console.log(label)
return (
&lt;View style={styles.userContainer}&gt;
&lt;View style={styles.bio}&gt;
&lt;IconButton name=&quot;account-circle&quot; size={70} style={styles.pp} /&gt;
&lt;View style={styles.cred}&gt;
&lt;Text style={styles.text}&gt;{label}&lt;/Text&gt;
&lt;Text style={styles.text}&gt;{route.params.email}&lt;/Text&gt;
&lt;Text style={styles.text}&gt;You have currently &lt;Text style={styles.bold}&gt;{route.params.notes.length}&lt;/Text&gt;  notes.&lt;/Text&gt;
&lt;/View&gt;
&lt;/View&gt;
&lt;View style={styles.bc}&gt;
&lt;Button title=&#39;Delete Account&#39; color={&#39;red&#39;} onPress={signOutHandler} /&gt;
&lt;Button title=&#39;Sign Out&#39; onPress={signOutHandler} /&gt;
&lt;/View&gt;
&lt;/View&gt;
)
}
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.

huangapple
  • 本文由 发表于 2023年3月7日 00:39:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定