简体   繁体   English

如何在 DApp 前端中使用 HTML 列出一些来自智能合约的数据

[英]How to list with HTML some data coming from Smart Contract in a DApp-Frontend

I have DApp with a Smart Contract which is holding some offers (id, price, owner etc.).我有一个带有智能合约的 DApp,它持有一些报价(id、价格、所有者等)。 I want to show that offers in my DApp-Frontend.我想在我的 DApp 前端显示该优惠。

First I am calling the Smart Contract and fetching all offers into an array with JavaScript:首先,我调用智能合约并使用 JavaScript 将所有报价提取到一个数组中:

// Load offers
      for (var i = 1; i <= offerCount; i++) {
        const offer = await contract.methods.offers(i).call()
        this.setState({
          offers: [...this.state.offers, offer]
        })
      }

Then I want to show the content of that array in a table:然后我想在表格中显示该数组的内容:

                <table className="table">
                  <thead>
                    <tr>
                      <th scope="col">#</th>
                      <th scope="col">Price</th>
                      <th scope="col">Owner</th>
                      <th scope="col"></th>
                    </tr>
                  </thead>
                  <tbody>
                    {
                      this.props.offers.map((offer, key) => {
                        return (
                          <tr key={key}>
                            <th scope="row">{offer.id.toString()}</th>
                            <td>{this.state.offers}</td>
                          </tr>
                        )
                      })
                    }
                  </tbody>
                </table>

I am getting the error TypeError: Cannot read property 'map' of undefined我收到错误TypeError: Cannot read property 'map' of undefined

I don't know how to show the data properly on the table.我不知道如何在表格上正确显示数据。

Library: React图书馆:反应

OS: XUbuntu操作系统:XUbuntu

Browser: Chrome浏览器:Chrome

TypeError: Cannot read property 'map' of undefined类型错误:无法读取未定义的属性“地图”

This error is because this.props.offers is undefined.这个错误是因为 this.props.offers 是未定义的。

You have to use this.state.offers, I think.我认为您必须使用 this.state.offers。

                {
                  this.state.offers.map((offer, key) => {
                    return (
                      <tr key={key}>
                        <th scope="row">{offer.id.toString()}</th>
                        <td>{this.state.offers}</td>
                      </tr>
                    )
                  })
                }

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

相关问题 如何通过来自智能合约的数据 map - How to map through data coming from smart contract 我无法将数据从智能合约提取到 HTML - I can't draw data from smart contract to HTML 如何从 Javascript 向 Solidity 智能合约 function 输入数据? - How to input data to a solidity smart contract function from Javascript? 是否可以从特定帐户在我的前端运行我的 Solidity 智能合约 function - Is it possible to run my Solidity Smart Contract function on my frontend from a specific account 如果没有数据来自json,则禁用html中的下拉列表 - make drop down list in html disabled if no data is coming from json 如果 html 数据来自 API 而不是 JSON 响应,如何插入手风琴图标并进行列表切换? - How to insert an accordion icon and do list toggling, if html data is coming from API instead of JSON response? 没有数据“网络”:{},在智能合约中。json - There is no data "networks": {}, in Smart contract .json 如何使用来自reducer的一些数据更新组件的状态 - How to update state of component with some data which is coming from reducer 如何在 javascript 中使用来自 lua 文件的一些数据? - How to use in javascript some data coming from lua files? 如何从js访问智能合约的公共地址? - How to access public address of smart contract from js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM