简体   繁体   English

map图片源嵌套JSON object在react js中如何实现?

[英]How to map Image source in nested JSON object in react js?

I had problem in showing my fetched image data from my api. from my console it didn't show any error but the card still not showing any image.我在显示从我的 api 获取的图像数据时遇到问题。从我的控制台它没有显示任何错误,但卡仍然没有显示任何图像。 How can I fix this?我怎样才能解决这个问题? I think it's because the Image is registered as another object unlike category and brand.我认为这是因为图像注册为另一个 object 不同于类别和品牌。

my Json Data Example:我的 Json 数据示例:

{
    "count": 5,
    "next": null,
    "previous": null,
    "results": [
        {
            "id": 5,
            "slug": "flashdisk-robot-16gb",
            "sku": "dsadsa232232",
            "name": "Flashdisk  Robot 16Gb",
            "description": "Ini adalah flashdisk",
            "weight": 6,
            "price": 30000,
            "stock": 0,
            "datetime_added": "2022-03-13T11:28:25.575695Z",
            "images": [
                {
                    "id": 1,
                    "image": "http://127.0.0.1:8000/products/PhotoRoom-20211115_151817.png"
                }
            ],
            "brand": {
                "id": 1,
                "slug": "lg",
                "name": "LG",
                "image": null
            },
            "category": {
                "id": 1,
                "slug": "electronic",
                "name": "Electronic",
                "image": null
            }
        }
    ]
}

My Code in React:我在 React 中的代码:

  useEffect(() =>{
    axios.get(`http://127.0.0.1:8000/data/products/?format=json`).then(res => {
        const products = Object.values(res.data);
        var filteredCategory = products[3].filter((product) => product.category.slug === categoryslug)
        setProductList(filteredCategory)
      })
  }, []);

console.log(productList)
  return (
    <>
      <Row>
        {productList && productList.map(product =>{
          const {id, name, price, category,images, slug} = product;
          return(
          <Col lg={3} key={id} className="d-flex">
            <Link className='card-product d-flex' to={`/product/${category.slug}/${slug}`}>
              <Card  className="flex-fill productlist">
                <Card.Img variant="top" src={images.image}/>
              </Card>
            </Link>
          </Col>
          )
        })}
      </Row>
    </>
  )

images is an array of objects: images是一个对象数组:

"images": [
  {
    "id": 1,
    "image": "http://127.0.0.1:8000/products/PhotoRoom-20211115_151817.png"
  }
],

so you have to do images[0].image for it to work.所以你必须做images[0].image才能工作。

<Card.Img variant="top" src={images[0].image}/>
<Row>
        {productList && productList.map(product =>{
          const {id, name, price, category,images, slug} = product;
          return(
          <Col lg={3} key={id} className="d-flex">
            <Link className='card-product d-flex' to={`/product/${category.slug}/${slug}`}>
              <Card  className="flex-fill productlist">
                {
                  images.map((val,index)=><Card.Img key={index} variant="top" 
                     src={val.image}/>
                }
              </Card>
            </Link>
          </Col>
          )
        })}
      </Row>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM