我有一个交易的合同,“要求”只有业主可以打电话。在前端,当我用合同所有者以外的帐户调用它时,我想显示一个错误。下面的代码不起作用。上面写着
错误:无效的JSON RPC响应:{"id":6,"jsonrpc":"2.0"}
this.state.web3.eth.getAccounts((error, accounts) => {
TalioInterview.deployed().then((instance) => {
try {
return instance.isTalioOwner.call({from:accounts[1]})
}catch(err){
alert("You are not authorized to run this.");
}
我的合同如下
function isTalioOwner()
public onlyIfTalioOwner()
constant
returns(bool)
{
return true;
}
在这里发现了一个相似问题,但没有任何答案。除了更改合同为非所有者帐户返回假外,这里最好的解决方案是什么?
发布于 2018-02-16 16:07:01
我对糖糖不太熟悉。(我想这是糖果,对吧?)但根据承诺通常的运作方式,我认为你想要这样的东西:
instance.isTalioOwner.call({from:accounts[1]}).then((result) => {
console.log("Success! Got result: " + result);
}).catch((err) => {
console.log("Failed with error: " + err);
});
发布于 2018-02-16 17:06:22
松露控制台将支持内部web3js,有和没有承诺。那么,当您使用call时,它将变成异步调用。因此,它将寻找回调/允诺方法。
this.state.web3.eth.getAccounts((error, accounts) => {
TalioInterview.deployed().then((instance) => {
try{
var result= instance.isTalioOwner({from:accounts[1]})
}catch(e){
console.log("error",e);
}
}));
Web3js可以工作,但是人们会推荐使用异步方法,因为您不需要等待下一个指令的执行。当您编写测试用例时,您可以在需要时使用上述方法,这将减少金字塔代码。
请参阅下面的屏幕快照:
https://ethereum.stackexchange.com/questions/39920
复制相似问题