[英]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
result[2]
)result[2]
时就不会遇到“越界”错误)view
or pure
function in Solidityview
或pure
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.