I have a transaction in contract which "requires" only owner can call. In front end when I call this with accounts other than owner of the contract I want to display an error. Code below doesn't work. It says

:Uncaught (in promise) Error: Invalid JSON RPC response: {"id":6,"jsonrpc":"2.0"}

      this.state.web3.eth.getAccounts((error, accounts) => {
        TalioInterview.deployed().then((instance) => {
          try {
            return instance.isTalioOwner.call({from:accounts[1]})

            alert("You are not authorized to run this.");

My contract looks as below

 function isTalioOwner()
    public onlyIfTalioOwner()
        return true;

Found a similar question here, but without any answer. What is the best solution here other than changing contract to return false for non-owner accounts ?


I'm not too familiar with Truffle. (I assume this is Truffle, right?) But just based on how Promises usually work, I think you want something like this:

instance.isTalioOwner.call({from:accounts[1]}).then((result) => {
  console.log("Success! Got result: " + result);
}).catch((err) => {
  console.log("Failed with error: " + err);
| improve this answer | |

Truffle console will support internally web3js, with and without promises also. When you use call then, it will become async call. So it will look for callback/promise method.

 this.state.web3.eth.getAccounts((error, accounts) => {
    TalioInterview.deployed().then((instance) => {
            var result= instance.isTalioOwner({from:accounts[1]})


Web3js will work, but people will recommend to use async method, because you no need to wait for next instruction to be execute. When your writing test cases you can use above method when it required, that will reduce pyramid code.

Find below screen shot for ref: enter image description here

| improve this answer | |
  • As of today, the version of truffle is not compatible with the async version of promise responses and updating the web3js then breaks truffle...but overall a good idea. – ReyHaynes Feb 16 '18 at 18:53
  • @Ismael It depends on truffle web3 connector. Its not necessarily promise method. I am updating my screen shot. – Jitendra Kumar. Balla Feb 17 '18 at 4:25
  • Updated my ans, Why -1? – Jitendra Kumar. Balla Feb 17 '18 at 4:28
  • @Ismael, My previous ans is correct, I use to use the same thing in my projects. When its req's non sync call. Its not always will return promise – Jitendra Kumar. Balla Feb 17 '18 at 4:37
  • It works with web3 v0.20, but it is bad practice. It will no longer work with web3 v1.0 and it doesn't work with truffle, since it uses promises. – Ismael Feb 17 '18 at 23:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.