简体   繁体   English

我无法将数据从智能合约提取到 HTML

[英]I can't draw data from smart contract to HTML

This is the error:这是错误:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '1')未捕获(承诺)TypeError:无法读取未定义的属性(读取“1”)

Here is the function that I'm using for drawing data from smart contract (and I call this function like getCandidate(1)):这是我用于从智能合约中提取数据的 function(我称此 function 为 getCandidate(1)):

async function getCandidate(cad){
    await myContract.methods.adaylar(cad);{
        var result;
        console.log("result : ", result);
        document.getElementById("cad" + cad).innerHTML = result[1];
        document.getElementById("cad"+cad+'count').innerHTML = result[2].toNumber();
        
    };
}

Assuming that the adaylar() function假设adaylar() function

  • returns an array or a struct with at least 3 items (so that you don't run into "out of bounds" error while accessing result[2] )返回至少包含 3 个项目的数组或结构(这样您在访问result[2]时就不会遇到“越界”错误)
  • and that it's a view or pure function in Solidity并且它是 Solidity 中的viewpure function

there are two issues in your code:您的代码中有两个问题:

The web3js library requires you to explicitly state if you want to make a (read-only) call() or send() a (read-write) transaction.如果您想进行(只读) call()send() ) 事务(读写), web3js库要求您显式输入 state。 Based on the assumption above, you'll want to make a call:基于上述假设,您需要拨打电话:

await myContract.methods.adaylar(cad).call();

This retrieves the returned value but doesn't store it anywhere in the JS code.这会检索返回值,但不会将其存储在 JS 代码中的任何位置。 So you'll need to store it in the result variable in order to access it:因此,您需要将其存储在result变量中才能访问它:

var result = await myContract.methods.adaylar(cad);
console.log("result : ", result);
document.getElementById("cad" + cad).innerHTML = result[1];
document.getElementById("cad"+cad+'count').innerHTML = result[2].toNumber();

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

相关问题 Reactjs读不懂智能合约? - Reactjs can't read smart contract? 如何在 DApp 前端中使用 HTML 列出一些来自智能合约的数据 - How to list with HTML some data coming from Smart Contract in a DApp-Frontend 如何实现一个实际上不铸造 NFT 的惰性铸造智能合约? - How can I implement a lazy-minting smart contract that doesn't actually mint NFTs? React,Solidity,Ethereum:我无法创建可正确调用智能合约功能的React按钮 - React, Solidity, Ethereum: I can't create a React button that correctly calls a smart contract function 错误:发送标头后无法设置标头。 来自调用智能合约功能 - Error: Can't set headers after they are sent. comes from calling the smart contract function 如何在 Flow 区块链上收听来自我的智能合约的事件? - How can I listen to an event from my smart contract on Flow blockchain? 如何从 Javascript 向 Solidity 智能合约 function 输入数据? - How to input data to a solidity smart contract function from Javascript? 如何通过来自智能合约的数据 map - How to map through data coming from smart contract 在 JavaScript 中使用 ethereum.request 从智能合约中检索数据 - Using ethereum.request to Retrieve Data from Smart Contract in JavaScript 没有数据“网络”:{},在智能合约中。json - There is no data "networks": {}, in Smart contract .json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM