TransWikia.com

Metamask Web3js Synchronous Method Error

Ethereum Asked on December 27, 2021

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.

One Answer

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?

Answered by Aniket on December 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP