Dapp-Development

無法訪問合約中的變數

  • April 2, 2017

這是我的契約——

pragma solidity ^0.4.0;
contract helloworld { 
    uint public balance;
   function helloworld(){
       balance = 1000;
   }

   function getBalance()  returns(uint) {
       return balance; 
   } 
}

我執行了 truffle compile 和 truffle migrate。然後我試圖通過訪問 balance 變數

helloworld.deployed().balance.call();

我收到以下錯誤-

TypeError: Cannot read property 'call' of undefined
   at evalmachine.<anonymous>:1:30
   at ContextifyScript.Script.runInContext (vm.js:35:29)
   at Object.runInContext (vm.js:89:6)
   at TruffleInterpreter.interpret (C:\Users\ujjwa\AppData\Roaming\npm\node_modules\truffle\lib\repl.js:136:17)
   at bound (domain.js:280:14)
   at REPLServer.runBound [as eval] (domain.js:293:12)
   at REPLServer.<anonymous> (repl.js:538:10)
   at emitOne (events.js:96:13)
   at REPLServer.emit (events.js:188:7)
   at REPLServer.Interface._onLine (readline.js:230:10)

如果你使用 truffle V3.x ( truffle version) 試試

helloworld.deployed().then(function(instance){helloworld=instance})

然後

helloworld.balance.call()

引用自:https://ethereum.stackexchange.com/questions/13836