简体   繁体   English

获取 2 API 并将两者的结果一起显示。 (反应.JS)

[英]Fetch 2 API and show the both result together. (React.JS)

I'm new to react.js, and currently conducting a project in my university.我是 react.js 的新手,目前在我的大学进行一个项目。 I am facing some problem regarding to Reactjs.我在 Reactjs 方面遇到了一些问题。 Hopefully you guys can guide me to solve this.希望你们能指导我解决这个问题。

So I have an Item Listing API which is gonna display in card component.所以我有一个项目列表 API,它将显示在卡片组件中。 I also have an API for Item Photo View, which is actually pass the id from Listing API and also will show in card component.我还有一个用于 Item Photo View 的 API,它实际上是从 Listing API 传递 id 并且也会显示在卡片组件中。 So each item has differents image.所以每个项目都有不同的图像。 Therefore, I am gonna call this two API in my react and I will see all the Item in the UI include their images.因此,我将在我的反应中调用这两个 API,我将看到 UI 中的所有项目都包含它们的图像。

This is Listing result from console log.:这是来自控制台日志的列表结果。:

在此处输入图片说明

Every item have different photo, so I create another API to view the item photo according to their id每个项目都有不同的照片,所以我创建了另一个 API 来根据他们的 id 查看项目照片

Here is my front-end code, how I call the API:这是我的前端代码,我如何调用 API:

    componentDidMount(){
        // console.log(loginEmail)
          fetch(`http://localhost:9000/api/item/list`,)
          .then((resp)=>{
            resp.json().then((res)=>{
             console.log(res.data);
              this.setState({data: res.data});

            }

            )
          })

          const id = this.state.data.id;

          fetch(`http://localhost:9000/api/item/photo/view/${id}`,)
          .then((resp)=>{
            resp.json().then((res)=>{
             console.log(res);
              this.setState({res});}
            )
          })
        }

Render component:渲染组件:

    render() {
        const data = this.state.data;
        return (
                <>
                    <div className="container my-5" >
                        <div className="row">
                            <div className="col-10 mx-auto col-md-6 text-center text-uppercase mb-3">
                                <h1 className="text-slanted">Item list</h1>
                            </div>
                        </div>
                        <div className="row">
                        <div className="col-10 mx-auto col-md-6 col-lg-4 my-3">
                        {
                                Object.keys(data).map((key) => 
                                <div className="card">
                                    {/* <img src="../{data[key].filename}" alt="Product"/> */} //no idea for the this, how to call the image
                                <div className="card-body text-capitalize">
                                <h6>{ data[key].organisation_name}</h6>
                                    <h6>asdasdaasd</h6>
                                </div>
                                <div className="card-footer">
                                    <button type="button" className="btn btn-primary text-capitalize" >
                                        details
                                    </button>
                                    Item Detail
                                </div>
                                </div>
                                )
                        }
                        </div>
                        </div>
                    </div>
                </>
            );
    }
}

The first Listing API is able to run, and the detail of item are able to show in card.第一个Listing API 可以运行,并且可以在卡片中显示项目的详细信息。 But, I can't get the result of Second API(Item Image View).但是,我无法获得第二个 API(项目图像视图)的结果。 The image could not shown out, and getting the error of this on my preview图像无法显示,并在我的预览中得到这个错误

在此处输入图片说明

Is there any place I'm wrong?有什么地方我错了吗? or there is another way that I can get the image from 2nd API and put it in card?或者还有另一种方法可以从第二个 API 获取图像并将其放入卡片中?

Put inside of the first API call the second API call:将第一个 API 调用放在第二个 API 调用中:

componentDidMount(){
        // console.log(loginEmail)
          fetch(`http://localhost:9000/api/item/list`,)
          .then((resp)=>{
            resp.json().then((res)=>{
             console.log(res.data);
              this.setState({data: res.data});
              const id = data.id;

              fetch(`http://localhost:9000/api/item/photo/view/${id}`,)
              .then((resp)=>{
                resp.json().then((res)=>{
                console.log(res);
                  this.setState({res});}
                )
              })

            }

            )
          })
        }

Or use async/await syntax to improve the readability.或者使用 async/await 语法来提高可读性。

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

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