0

I currently doing a react app, I have to display the product image for each product, the product image is set as a ProductImagesID. To get the product image ID it needs to call an API.So I try to call directly in "src", but I don't know to replace the product. id and imageID with the data from the query to make it dynamic to show the photos for the specific product. Plz help me on how make the api in the src dynamic. With this i can learn somethings new to improve my knowledge. Thanks in Advance.

This how I try to call the API directly in the src

This how I try to call the API directly in the src

This the How the Productimage API with the productID and the ImageID.

ProductImage API path

This is the ProductImages are set in array of ID

enter image description here

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.1/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/15.0.1/react-dom.min.js"></script>
<div>
      <div className="wrapper">
        <div className="section latest-offers">
          <Container>
            <Row xs="1" sm="2" md="3">
              {res.data.Result.slice(0, 3).map((product) => {
                return (
                  <Col key={product.ID} xm="12" md={{ size: "3", offset: 1 }}>
                    <Card className="card-product card-plain d-flex flex-column p-3 flex-md-row  flex-sm-column">
                      <div className="card-image p-2 ">
                        <img
                          className=""
                          alt={product.Name}
                          src="/api/v1/ProductFile/?productID={610a454ad51ba524b7f949f9}/?get=Product/?imageID={610a4582d51ba524b7f949fa}"
                        />

                        <CardBody>
                          <div className="card-description font-weight-normal">
                            <CardTitle tag="h5">{product.Name}</CardTitle>
                          </div>
                          <div className="price">
                            <span className="mr-1">
                              <strong>
                                RM
                                {product.Price}
                              </strong>
                            </span>
                          </div>
                        </CardBody>
                      </div>
                    </Card>
                  </Col>
                );
              })}
            </Row>
          </Container>
        </div>
      </div>
    </div>

1

1 Answer 1

2

Your Swagger screenshot indicates you need path parameters, not query parameters.

Try something like this

<img src={`/api/v1/ProductFile/${encodeURIComponent(product.ID)}/Product/${encodeURIComponent(product.ProductImages[0])}`}>

Note that I've run the parameters through encodeURIComponent() to make sure they won't break your URLs.


If you wanted to show all your images, you could use the map method

{product.ProductImages.map(image => (
  <img src={`/api/v1/ProductFile/${encodeURIComponent(product.ID)}/Product/${encodeURIComponent(image)}`} />
))}
Sign up to request clarification or add additional context in comments.

5 Comments

sorry if I asking a lot of questions, as in swagger you can see the ProductImages are in an array set in ID, how should I do to display only the 1st image of the array. Can you plz teach me?
@Lucky something like product.ProductImages[0] perhaps? I've updated my answer
thank you so much, It's work. Thank you so much, I have learned new things today .
@Phill wanted to ask if want to display all the images in the array. should I do like this product.ProductImages[ ] , plz help .
@Lucky I've added an example to my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.