1

Okay guys, I'm a complete newbie to Ethereum and Web3.js and going through the Crypto Zombies tutorial and have done okay so far.

But, when I use this function in my index.html file as they show it I get the

Uncaught Error: The MetaMask Web3 object does not support synchronous methods like eth_call without a callback parameter.

Here's the function:

function getZombiesByOwner(owner) {
        return cryptoZombies.methods.getZombiesByOwner(owner).call()
      }

Contract Function Code:

function getZombiesByOwner(address _owner) external view 
returns(uint[]) { 
  uint[] memory result = new uint[](ownerZombieCount[_owner]); 
  uint counter = 0; 
  for (uint i = 0; i < zombies.length; i++) { 
   if (zombieToOwner[i] == _owner) { 
    result[counter] = i; counter++; 
   } 
  } 
return result; 
}

I get that it wants me to use a callback and I've looked at every example/tutorial I could find for the last 2 days. None of the examples that I've tried to replicate have worked.

Is there anyone here who could show me how that function would be written as an asynchronous call instead of the synchronous way I have it here? I'd appreciate it very much because I don't think I've ever felt this stupid in my whole life.

Thanks in advance.

0

You can use it this way.

 cryptoZombies.methods.getZombiesByOwner(owner).call({from: <fromAddress>}, 
    function(error, result){
        if(!error)
            console.log(result)
         else
            console.error(error);
    });

EDIT: Put your ethereum account address at the place of <fromAddress>

This is similar to this one: Is there a way to call a constant function asynchronously?

  • Thanks for the assistance. I just replaced the function the way I originally had it with your recommendation. No longer getting that synchronous error in the console but now it gives me: Uncaught SyntaxError: Unexpected token < When I click on that in the console it has this part highlighted: <fromAddress>}, – Kevin Phillips Dec 6 '18 at 6:33
  • I have updated the answer regarding this – Aniket Dec 6 '18 at 6:36
  • Thanks so much for your patience with my ignorance. Replaced that and now getting a "Invalid number of arguments to Solidity function" error. I'm starting to think I'm better suited to digging ditches than learning this stuff. – Kevin Phillips Dec 6 '18 at 6:46
  • Update code of the solidity function in the question. – Aniket Dec 6 '18 at 7:15
  • Here's the solidity code. Looks like the code in the function above is using the argument "owner" and the solidity code is looking for "owner". 'function getZombiesByOwner(address _owner) external view returns(uint[]) { uint[] memory result = new uint[](ownerZombieCount[_owner]); uint counter = 0; for (uint i = 0; i < zombies.length; i++) { if (zombieToOwner[i] == _owner) { result[counter] = i; counter++; } } return result; }' – Kevin Phillips Dec 6 '18 at 7:27

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.