我可以从智能合约到html绘制数据

发布于 2025-02-02 03:12:22 字数 476 浏览 1 评论 0原文

这是错误:

undured(在承诺中)TypeError:无法读取未定义的属性(读取'1')

这是我正在使用的功能来绘制智能合约的数据(我称此功能为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();
        
    };
}

This is the error:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '1')

Here is the function that I'm using for drawing data from smart contract (and I call this function like 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();
        
    };
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

林空鹿饮溪 2025-02-09 03:12:22

假设adaylar()函数

  • 返回至少3个项目的数组或结构(因此,在访问结果[2] <时,您不会遇到“限制”错误。 /code>)
  • 并且它是view固体中的功能,

您的代码中有两个问题:

web3js 库库要求您明确说明(如果您要制作(只读)call> call()send> send() send()< /code> a(读写)事务。基于上面的假设,您需要拨打电话:

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();

Assuming that the 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])
  • and that it's a view or pure function in Solidity

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. 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. So you'll need to store it in the result variable in order to access it:

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文