使用 truffle 在solidity 中调用函数时出现新的 BigNumber() 错误。如何修复错误?

Posted

技术标签:

【中文标题】使用 truffle 在solidity 中调用函数时出现新的 BigNumber() 错误。如何修复错误?【英文标题】:Getting new BigNumber() error when calling a function in solidity using truffle. How do I fix the error? 【发布时间】:2018-07-20 12:28:02 【问题描述】:

当我尝试使用 truffle 调用我的solidity 函数时出现此错误。

我的 Solidity 代码如下:

pragma solidity ^0.4.14;

contract SimpleDemo 
    function returnNumber () public view returns (uint) 
        return 500;
    

我打电话给returnNumber() 的方式是:

this.state.web3.eth.getAccounts((error, accounts) => 
    simpleDemo.deployed().then((instance) => 
        simpleDemoInstance = instance
        // Below line runs with the error ...
        return simpleDemoInstance.returnNumber.call()
    ).then((result) => 
        console.log(result)
    )
)

另外,this solution 根本没有帮助。因此,我单独询问。

【问题讨论】:

在我看来,您收到的是 [Object object] 而不是 500 原语。您使用.call() 而不仅仅是returnNumber() 有什么原因吗?您可以在 Promise 链的底部添加 .catch(..) 以进行更多检查。 其实我也试过returnNumber(),但同样的错误仍然存​​在。我马上试试catch(),看看有没有问题!! @IanMacDonald 我尝试使用catch() 检查一些错误,但返回了一个对象。我不知道如何返回对象。对象是 "name": "BigNumber Error" 。现在我应该如何处理这个错误。 :P 【参考方案1】:

应该是:

simpleDemoInstance.methods.returnNumber().call( from: accounts[0] );

如果它是一个需要气体的函数(假设你想从元掩码发送)。

如果不是你使用的支付功能:

simpleDemoInstance.methods.returnNumber().call()

也使用 es6。尝试在没有异步等待的情况下编写这些东西是可怕的 IMO。

【讨论】:

以上是关于使用 truffle 在solidity 中调用函数时出现新的 BigNumber() 错误。如何修复错误?的主要内容,如果未能解决你的问题,请参考以下文章

solidity 智能合约(3):使用truffle编译部署及测试合约

以太坊Solidity之Truffle框架的应用实践

如何调用 Solidity 函数从智能合约中返回以太币?

Solidity 智能合约迁移错误的 Truffle 和 Ganache 教程

用Solidity在Truffle上构建一个HelloWorld智能合约

区块链投票应用:使用solidity+truffle+metamsk开发Dapp应用