Hello world contract方法调用返回Unhandled rejection Error

时间:2017-09-22 03:39:59

标签: solidity smartcontracts

这是我的package.json

{
  "dependencies": {
    "solc": "^0.4.16",
    "web3": "^1.0.0-beta.20"
  }
}

这是我的智能合约代码:

contract HelloWorld {
    function displayMessage() constant returns (string) {
        return "Hello from a smart contract";
    }
}

我已经在运行testprc的节点repl上编译了这个合约,我正试图像这样调用displayMessage方法:

myContract.methods.displayMessage().call()

myContract.methods.displayMessage.call()

但我刚收到这个错误:

> Unhandled rejection Error: ERROR: The returned value is not a convertible string:
at SolidityTypeString.formatOutputString [as _outputFormatter] (/Users/alberthu/Documents/solidity-intro/node_modules/web3-eth-abi/src/formatters.js:245:15)
at /Users/alberthu/Documents/solidity-intro/node_modules/web3-eth-abi/src/type.js:246:25
at SolidityTypeString.SolidityType.decode (/Users/alberthu/Documents/solidity-intro/node_modules/web3-eth-abi/src/type.js:247:11)
at /Users/alberthu/Documents/solidity-intro/node_modules/web3-eth-abi/src/index.js:327:49
at Array.forEach (<anonymous>)
at ABICoder.decodeParameters (/Users/alberthu/Documents/solidity-intro/node_modules/web3-eth-abi/src/index.js:326:13)
at Contract._decodeMethodReturn (/Users/alberthu/Documents/solidity-intro/node_modules/web3-eth-contract/src/index.js:490:22)
at Method._parent._ethereumCall.call.method.outputFormatter (/Users/alberthu/Documents/solidity-intro/node_modules/web3-eth-contract/src/index.js:820:42)
at Method.formatOutput (/Users/alberthu/Documents/solidity-intro/node_modules/web3-core-method/src/index.js:179:54)
at sendTxCallback (/Users/alberthu/Documents/solidity-intro/node_modules/web3-core-method/src/index.js:446:33)
at /Users/alberthu/Documents/solidity-intro/node_modules/web3-core-requestmanager/src/index.js:144:9
at XMLHttpRequest.request.onreadystatechange (/Users/alberthu/Documents/solidity-intro/node_modules/web3-providers-http/src/index.js:64:13)
at XMLHttpRequestEventTarget.dispatchEvent (/Users/alberthu/Documents/solidity-intro/node_modules/xhr2/lib/xhr2.js:64:18)
at XMLHttpRequest._setReadyState (/Users/alberthu/Documents/solidity-intro/node_modules/xhr2/lib/xhr2.js:354:12)
at XMLHttpRequest._onHttpResponseEnd (/Users/alberthu/Documents/solidity-intro/node_modules/xhr2/lib/xhr2.js:509:12)
at IncomingMessage.<anonymous> (/Users/alberthu/Documents/solidity-intro/node_modules/xhr2/lib/xhr2.js:469:24)
at emitNone (events.js:110:20)
at IncomingMessage.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1059:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickDomainCallback (internal/process/next_tick.js:218:9)

我试过通过web3 api文档阅读,但我是新手,并没有找到任何有用的东西。任何人都知道如何在服务器上显示“来自智能合约的Hello”吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

我也在学习如何编写智能合约,并在今天早些时候遇到了这个错误。我发现造成这种情况的原因是我的错误地址与我的智能合约有关。

web3中的合同对象具有地址属性,该属性表示用于该合同实例的地址。在我的情况下,这个地址不正确,导致我有同样的错误。部署后应该已经将合同地址退回给您。

在部署之后,应该在回调函数中将正确的合同地址返回给您。

您可以按如下方式设置和检查合同地址:

myContract.options.address // returns the current address you have set for this contract instance
myContract.options.address = "the correct contract address"

我会留下这个评论,但不幸的是我没有足够的声誉。如果您需要更多帮助或想要聚在一起学习,请告诉我。

以下是合约地址属性的web3js文档:https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#options-address