【问题标题】:Why I can access my contract in one function but not another one为什么我可以在一个功能中访问我的合同,但不能在另一个功能中访问我的合同
【发布时间】:2020-07-30 15:13:12
【问题描述】:

如果标题有点混乱,我很抱歉。

我正在开发带有智能合约的以太坊区块链。

App={
 createVoter1: function(){
        App.contracts.Voting.deployed().then(function(instance) {
            // use the contract function createBallot
            var college = $("#college1").text();
            var major = $("#major1").text();
            var expirydate = $("#expirydate").text();
            var serialnumber = $("#serialnumber1").text();
            instance.createVoter(web3.eth.accounts[0],major,college,expirydate,serialnumber,"",[]);
        })
    },
    ifVoterAddressExists: function(){
      App.contracts.Voting.deployed().then(function(instance){
        instance.ifVoterAddressExists(web3.eth.accounts[0]);
      })
    }
}

我在 createVoter1 函数之前跳过了一些代码。问题是,createVoter1 函数可以成功使用合约的函数。但是,当我使用 ifVoterAddressExists 时,会出现 Uncaught TypeError: Cannot read property 'deployed' of undefined 的警告。我可以知道是否有任何可能的解决方案?谢谢!

【问题讨论】:

    标签: javascript ethereum solidity smartcontracts web3js


    【解决方案1】:

    您的代码在部署之外执行。您需要先等待合约部署完成。

    【讨论】:

    • 感谢您的回答。您能否进一步解释我如何“等待部署合约”?
    猜你喜欢
    • 2016-10-19
    • 2019-11-17
    • 2014-03-28
    • 1970-01-01
    • 1970-01-01
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    • 2013-05-25
    相关资源
    最近更新 更多