简体   繁体   English

TypeError: undefined is not an object(评估 XYZ)

[英]TypeError: undefined is not an object (evaluating XYZ)

I cannot seem to shake this error and I don't know what I'm doing wrong.我似乎无法摆脱这个错误,我不知道我做错了什么。 I have a 'show' screen displaying a selected post.我有一个显示选定帖子的“显示”屏幕。 At the top of the screen is a profile picture for the user that posted.屏幕顶部是发布用户的个人资料图片。

When the component is mounted, details of the select post are retrieved and stored in a state ("post").安装组件后,将检索 select 帖子的详细信息并将其存储在 state(“帖子”)中。 This post-state includes a user object which contains information about the user that made the post - including, in some cases, their profile picture.此帖子状态包括用户 object,其中包含有关发布帖子的用户的信息 - 在某些情况下包括他们的个人资料图片。

As not all users have profile pictures, I use a simple ternary operator to determine whether a profile picture or an icon should be displayed:由于并非所有用户都有头像,我使用一个简单的三元运算符来确定是否应该显示头像或图标:

 const [post, setPost] = useState({})

    useEffect(() => {
        const post = feed.find(el => el.id === props.route.params.postId)
        setPost(post)
    }, [feed])


return <>
      <ListItem>
          { post.user.profilePic 
                ? <Avatar source={{ uri: post.user.profilePic }} size="medium" rounded />
                : <Avatar rounded icon={{ name: 'person', type: "ionicons" }} size="medium" rounded overlayContainerStyle={{ backgroundColor: 'grey' }} />}           
            <ListItem.Content>
                    <ListItem.Title style={{ fontWeight: "bold" }}>{"@" + post.user.userName}</ListItem.Title>
            </ListItem.Content>
        </ListItem>

Quite straightforward and this same code works fine in other components, but in this one, it keeps crashing:非常简单,同样的代码在其他组件中也能正常工作,但是在这个组件中,它一直在崩溃:

TypeError: undefined is not an object (evaluating 'post.user.profilePic')

This error is located at:
    in ShowScreen (at SceneView.tsx:122)
    in StaticContainer
    in StaticContainer (at 
etc

I have tried returning a loading component if the post hasn't fully loaded;如果帖子没有完全加载,我尝试返回加载组件; change the ternary operator to check if post.更改三元运算符以检查是否发布。 user.profile pic === undefined; user.profile pic === 未定义; using type and hasOwnProperty methods.使用 type 和 hasOwnProperty 方法。

Here is the 'post' as console.logged:这是console.logged的“帖子”:

Object {
  "caption": "Testing caption
",
  "comments": Array [
    Object {
      "authorId": "vqEzix1k4tQer7wmMG32Fby4KKg1",
      "authorName": "sarah123",
      "comment": "Comments test",
      "created": t {
        "nanoseconds": 119000000,
        "seconds": 1614335984,
      },
      "id": "sYCm2nk0XpDKYiBQ6fmy",
    },
    Object {
      "authorId": "vqEzix1k4tQer7wmMG32Fby4KKg1",
      "authorName": "rob123",
      "comment": "Testing comments works!",
      "created": t {
        "nanoseconds": 362000000,
        "seconds": 1614331033,
      },
      "id": "srd6faF3uw2AvQbWskzd",
    },
  ],
  "creation": t {
    "nanoseconds": 144000000,
    "seconds": 1612301025,
  },
  "currentUserLike": false,
  "downloadUrl": "https://firebasestorage.googleapis.com/v0/b/rn-instagram-clone-88f85.appspot.com/o/post%2F2qZfG0Wve6hmffdS0hL13qoIYp92%2F0.xgqi91loct?alt=media&token=ac9bc56f-d04b-46f2-acc6-45f8475134ab",
  "id": "PiFmGtUb55OMGUiIQO61",
  "location": "Bangor",
  "user": Object {
    "email": "test@gmail.com",
    "name": "test",
    "uid": "2qZfG0Wve6hmffdS0hL13qoIYp92",
  },
}

What am I doing wrong?我究竟做错了什么? Thanks.谢谢。

You should update post state when you have found the valid one.找到有效的帖子后,您应该更新 state 帖子。

 const [post, setPost] = useState({})

    useEffect(() => {
        const post = feed.find(el => el.id === props.route.params.postId)
        if (post) {
          setPost (post)
        }
    }, [feed])


return <>
      <ListItem>
          { post.user.profilePic 
                ? <Avatar source={{ uri: post.user.profilePic }} size="medium" rounded />
                : <Avatar rounded icon={{ name: 'person', type: "ionicons" }} size="medium" rounded overlayContainerStyle={{ backgroundColor: 'grey' }} />}           
            <ListItem.Content>
                    <ListItem.Title style={{ fontWeight: "bold" }}>{"@" + post.user.userName}</ListItem.Title>
            </ListItem.Content>
        </ListItem>

Try this way试试这个方法

<ListItem>
  {post && post.user && post.user.profilePic ? (
    <Avatar source={{ uri: post.user.profilePic }} size="medium" rounded />
  ) : (
    <Avatar
      rounded
      icon={{ name: "person", type: "ionicons" }}
      size="medium"
      rounded
      overlayContainerStyle={{ backgroundColor: "grey" }}
    />
  )}
  <ListItem.Content>
    <ListItem.Title style={{ fontWeight: "bold" }}>
      {post && post.user && post.user.userName ? "@" + post.user.userName :""}
    </ListItem.Title>
  </ListItem.Content>
</ListItem>
use null check post.user && post.user.profilePic

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:undefined不是对象(评估“ XYZ”)-JavaScript - TypeError: undefined is not an object (evaluating 'XYZ') - JavaScript TypeError: undefined is not an object(评估“this”) - TypeError: undefined is not an object (evaluating 'this') TypeError:未定义不是对象(正在评估“ table [0] [3]”) - TypeError: undefined is not an object (evaluating 'table[0][3]') TypeError:未定义不是对象(正在评估“木板”) - TypeError: undefined is not an object (evaluating 'board') TypeError: undefined is not an object(评估'object ['body']') - TypeError: undefined is not an object (evaluating 'object['body']') TypeError:undefined不是对象(评估“ this.text”) - TypeError: undefined is not an object (evaluating 'this.text') TypeError:未定义不是对象(评估“ this .__ reactAutoBindMap”) - TypeError: undefined is not an object (evaluating 'this.__reactAutoBindMap') TypeError:未定义不是对象(正在评估“ c.trigger”) - TypeError: undefined is not an object (evaluating 'c.trigger') typeError: undefined 不是一个对象(评估“item.phoneNumbers[0]”) - typeError: undefined is not an object (evaluating 'item.phoneNumbers[0]') 类型错误:未定义不是对象(评估&#39;_this3.state.bind&#39;) - TypeError: undefined is not an object (evaluating '_this3.state.bind')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM