Ethereum 使用Web3.js版本调用智能合约中的公共方法:';1.0.0-beta.46';

Ethereum 使用Web3.js版本调用智能合约中的公共方法:';1.0.0-beta.46';,ethereum,smartcontracts,web3js,parity-io,Ethereum,Smartcontracts,Web3js,Parity Io,我开始进入以太坊区块链的第一步,在私有网络上实现对等。我能够配置奇偶校验,并通过奇偶校验UI在我的专用网络上的开发模式链上执行智能合约的部署,奇偶校验UI还能够调用合约的方法 我面临的问题与使用Web3.js在智能合约中调用函数有关。我能够使用Web.js库连接到链 Web3 = require('web3') web3 = new Web3('ws://localhost:8546') mycontract = web3.eth.Contract([{"constant":false,"i

我开始进入以太坊区块链的第一步,在私有网络上实现对等。我能够配置奇偶校验,并通过奇偶校验UI在我的专用网络上的开发模式链上执行智能合约的部署,奇偶校验UI还能够调用合约的方法

我面临的问题与使用Web3.js在智能合约中调用函数有关。我能够使用Web.js库连接到链

Web3 = require('web3')

web3 = new Web3('ws://localhost:8546')

mycontract = web3.eth.Contract([{"constant":false,"inputs":[],"name":"greet","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"}],0xEb112542a487941805F7f3a04591F1D6b04D513c)
当我调用下面的方法时

mycontract.methods.greet().call()
它给我以下输出,而不是通过在智能合约函数中编写的承诺对象返回预期字符串“OK Computer”

{ [Function: anonymousFunction]
  send: { [Function] request: [Function] },
  estimateGas: [Function],
  encodeABI: [Function] }
智能合约代码:

pragma solidity ^0.4.22;
//Compiler Version: 0.4.22
contract Greeter {
    address owner;

    constructor() public { 
        owner = msg.sender; 
    }    
    function greet() public returns(string){
        return "OK Computer";
    }
}

涉及区块链状态变化的每个交易或智能合约方法调用将返回。因此,您只需相应地处理承诺:

mycontract.methods.greet.call().then(function(resp) {
   console.log(resp) // This will output "OK Computer"
}

更多信息请访问

感谢您的回复;最初的观点还与web3.jsapi的行为有关,即它不返回预期的Promise对象,而是按照最初的帖子返回对象;仍然按照您的建议,语句
mycontract.methods.greet().call().then(function(res){console.log(res)})
的输出是TypeError:mycontract.methods.myFunction(…).call(…)。then不是我在本地尝试过的函数,也不是mycontract.methods.greet.call().then(console.log)之后的函数;打印“Ok Computer”合同的地址应该是一个字符串,我在阅读文档时完全忽略了这个字符串。一旦用引号括起来,它就会按照你的建议工作。