Q: I need to call two APIs in Reactjs I have 2 APIs, one call the products and the other one call the image of the products.
Products API: https://inventory.dearsystems.com/ExternalApi/v2/Product
The second API is required call the ID of the products
Image API: https://inventory.dearsystems.com/ExternalApi/v2/product/attachments?ProductID=
How can I call the second one to show the image.
Here is my code:
import axios from "axios";
import {  useEffect, useState } from "react";
import { SingleContent } from "../../components/SingleContent/SingleContent";

export const All = () => {

    const [content, setContent] = useState([]);
  
    const fetchAllProducts = async () => {
        const  { data } = await axios.get('https://inventory.dearsystems.com/ExternalApi/v2/Product',{
            headers: {
                "api-auth-accountid": process.env.REACT_APP_API_ID,
                "api-auth-applicationkey": process.env.REACT_APP_API_KEY
              } 
        });

      
        console.log(data.Products);
        
        setContent(data.Products)
    
    }
   
    useEffect(() =>{
        fetchAllProducts();
        
    }, [])

    return (
        <div>
            <h1 className="pageTitle">All Products</h1>
            <div className="all">
                {content && content.map((c) => 
                    <SingleContent 
                        key={c.id} 
                        id={c.ID} 
                        name={c.Name} 
                        sku={c.SKU} 
                        category={c.Category}/> )}
                        
                       
            </div>
        </div>
    )
}






A: Inside fetchAllProducts() you could map the data.Products array you get from the first request, call the second api for each item and add the product image to the item.
Then you can update the contents with the resulting array.
Edited: example code below.


const fetchAllProducts = async () => {
        const  { data } = await axios.get('https://inventory.dearsystems.com/ExternalApi/v2/Product',{
            headers: {
                "api-auth-accountid": process.env.REACT_APP_API_ID,
                "api-auth-applicationkey": process.env.REACT_APP_API_KEY
              } 
        });

        const productsWithImage = data.Products
          .map(async product => {
            const imageUrl = await axios.get("https://inventory.dearsystems.com/ExternalApi/v2/product/attachments?ProductID=" + product.id)
            return {...product, imageUrl }
          })
        
        setContent(productsWithImage)
    }
    
// Then you can use product.imageUrl when you map your products



A: Use for instead of map because it was returning a Promise. The problem is that if setContent(result) is called outside of the for, it only returns on register.
See the code:
export const AllHandle = () => {

    const [content, setContent] = useState([]);

    const fetchAllProducts = async () => {
        
        const { data }  = await axios.get("https://inventory.dearsystems.com/ExternalApi/v2/Product?limit=10",{
            headers: {
                "api-auth-accountid": process.env.REACT_APP_API_ID,
                "api-auth-applicationkey": process.env.REACT_APP_API_KEY
              } 
        });
        let result = [];
        const totaProducts = await data.Products;

        for (let product of totaProducts) {
           
            const imageUrl = await axios.get(`https://inventory.dearsystems.com/ExternalApi/v2/product/attachments?ProductID=${product.ID}`, {
                headers: {
                    "api-auth-accountid": process.env.REACT_APP_API_ID,
                    "api-auth-applicationkey": process.env.REACT_APP_API_KEY
                  } 
              });
              
              const imgUrl = imageUrl.data
             
              result = [{ ...product, imgUrl}]
            }
           
            setContent(result)        
 }

 useEffect(() =>{
    fetchAllProducts();
}, [])   

 return (
    <div>
        <h1 className="pageTitle">All Products</h1>
        <div className="all">
            {content && content.map((c) => 
              
                <SingleContent 
                    key={c.ID} 
                    id={c.ID} 
                    name={c.Name} 
                    sku={c.SKU} 
                    category={c.Category}
                    attachment= { c.imgUrl[0].DownloadUrl}
                    />

                    )}
      
        </div>
    </div>
)

   
}

