【发布时间】:2017-12-21 00:49:06
【问题描述】:
我正在尝试让已部署的 HelloWorld 合同在节点应用程序中运行。我想像这样运行call() 函数来检查它:
const deployed = helloWorldContract.new({
from: acct1,
data: compiled.contracts[':HelloWorld'].bytecode,
gas: 151972,
gasPrice: 5
}, (error, contract) => {
if(!error){
console.log(contract.displayMessage.call());
} else {
console.log(error);
}
});
以下是合同供参考:
contract HelloWorld {
function displayMessage() public constant returns (string){
return "hello from smart contract - {name}";
}
}
当我在回调中尝试 console.log(contract.displayMessage.call()) 时,返回:TypeError: Cannot read property 'call' of undefined,但是,当我登录 console.log(contract.displayMessage) 时,它返回:
{ [Function: bound ]
request: [Function: bound ],
call: [Function: bound ],
sendTransaction: [Function: bound ],
estimateGas: [Function: bound ],
getData: [Function: bound ],
'': [Circular] }
我在这里做错了什么?如何在已部署的合约中运行 call 函数?
【问题讨论】:
-
它不是一个函数而不是一个属性吗?
-
正确。如果它是一个属性,我不会使用
contract.displayMessage.call访问吗?如果它是一个函数,我不能用contract.displayMessage.call()访问它吗?为了清楚起见,将合同代码添加到问题中 -
我的意思是 displayMessage?
-
是的,它是一个函数,在我的合同中定义,尽管
console.log(contract.displayMessage)将返回上面发布的对象。如果我自己运行console.log(contract.displayMessage());,我会得到:contract.displayMessage is not a function如果我运行console.log(contract.displayMessage().call());,我会得到:contract.displayMessage is not a function你有什么建议?