[英]I can't draw data from smart contract to HTML
这是错误:
未捕获(承诺)TypeError:无法读取未定义的属性(读取“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();
};
}
假设adaylar()
function
result[2]
时就不会遇到“越界”错误)view
或pure
function您的代码中有两个问题:
如果您想进行(只读) call()
或send()
) 事务(读写), web3js库要求您显式输入 state。 基于上述假设,您需要拨打电话:
await myContract.methods.adaylar(cad).call();
这会检索返回值,但不会将其存储在 JS 代码中的任何位置。 因此,您需要将其存储在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.