【问题标题】:display a photo in a list item undefined is not an object在列表项中显示照片 undefined is not an object
【发布时间】:2021-05-05 12:08:59
【问题描述】:

我有一个产品列表,使用 react-native-elements 中的列表项显示。 我想将产品图像添加为产品名称左侧的“图标”。 我遇到了困难

'未定义的不是对象'

当我调用图像时。 我不明白为什么会显示错误,我的数组如下所示:

Object {
    "cost": 1099.99,
    "created_at": "2018-05-17T20:58:31Z",
    "custom_fields": Array [],
    "description": "",
    "family_id": 5,
    "id": 5,
    "incl_tax": 1,
    "is_visible": 1,
    "name": "Apple MacBook Air 13.3'' LED 128 Go SSD 8 Go",
    "photo": Object {
      "_1_": Object {
        "id": 1,
        "order": 1,
        "title_fr": "Apple-MacBook-Air-13-3-LED-128-Go-D-8-Go-RAM-Intel-Core-i5-bicoeur-a-1-8-Ghz-MQD32FN-Nouveau",
        "url": "/i/p-4-6-5-146_5844_5_1.jpg",
      },
    },
    "quantity": "0",
    "reference": "",
    "stock_status": "0",
    "tax_rate_id": 1,
    "unit": "",
    "updated_at": "2018-06-27T10:43:46Z",
    "weight": 0,   },

所以我这样称呼我的照片:

1.url}} style={{ width: 25, height: 25}}/>

常量“URL”允许我在 url 的开头添加数组的 url 的结尾。 对我来说似乎是正确的,我不明白错误,你知道问题出在哪里吗?我真的需要弄清楚并解决这个问题。

感谢任何愿意花时间阅读我的帖子并帮助我的人。

这部分的代码:

<ListItem
    style={{width:'100%'}}
    containerStyle= {{backgroundColor: item % 2 === 0 ? '#000' : '#ccc'}}
    bottomDivider
    onPress={() => this.props.navigation.navigate('ProductDetails', {productId:parseInt(item.id)})}>
    <ListItem.Content style={{flexDirection: 'row', justifyContent: 'space-between'}}>
      <Image source={{uri:URL+ item.photo._1_.url}}
             style={{ width: 25, height: 25}}/>
      <ListItem.Title style={{width: '65%', fontSize: 16}}>{ ( item.name.length > 20 ) ? item.name.substring(0, 20) + ' ...'  :  item.name}</ListItem.Title>
      <ListItem.Subtitle style={{ color: '#F78400', position: "absolute", bottom: 0, right: 0 }}>{item.cost}€</ListItem.Subtitle>
    </ListItem.Content>
  </ListItem>

【问题讨论】:

    标签: javascript list image react-native photo


    【解决方案1】:

    看看这个

    <Image source={URL+ item.photo["_1_"].url} style={{ width: 25, height: 25}}/>
    

    【讨论】:

    • 感谢您抽出宝贵时间帮助我。我按照你的建议做了,它仍然返回:[未处理的承诺拒绝:TypeError:未定义不是对象(评估'item.photo[“1”].url')]
    猜你喜欢
    • 2020-08-15
    • 2019-03-25
    • 1970-01-01
    • 2020-08-07
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多