0

So i recently took over a project that had been abandoned for a while, which involves an ECR20 smart contract and the use of the web3 library to communicate with the smart contract. I've been trying for days to only call a read function or just a property, but I always get the following error:

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

No matter which function i call, it's always the same. The contract instance is fine because there is another call to writing function in the same script and i'm using that same contract instance, but I'm not sure what is the proper syntax to call the funcion, so far i've tried these approaches (trying to call whiteListedAddresses function):

console.log(Coursetro.whiteListedAddresses(address).call()); //try 1

console.log(Coursetro.whiteListedAddresses(address)); //try 2

Coursetro.whiteListedAddresses(address).then(console.log); //try 3

Coursetro.whiteListedAddresses(address).call().then(function (result) {
  console.log(result + ' is the result');
}).then( function () {
  console.log("final call");
}); //try 4

Coursetro.whiteListedAddresses().call({'address':address}, result =>{
console.log(result);
}); //try 5

And other approaches, but nothing works, i can't get to read anything. Anyone knows what is the proper way to read from a contract?

--EDIT--

Contract initialization:

var CoursetroContract = web3.eth.contract(ABI);

 var Coursetro = CoursetroContract.at(contractAddress);

Like i said, this is working fine since other functions are being called and they are working, but they are all writting functions, and i'm trying to call reading functions.

web3js version is 0.20.7

these 4:

console.log(Coursetro.whiteListedAddresses(address).call()); //try 1

console.log(Coursetro.whiteListedAddresses(address)); //try 2

Coursetro.whiteListedAddresses(address).then(console.log); //try 3

Coursetro.whiteListedAddresses(address).call().then(function (result) {
  console.log(result + ' is the result');
}).then( function () {
  console.log("final call");
}); //try 4

output:

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

and this one:

Coursetro.whiteListedAddresses().call({'address':address}, result =>{
console.log(result);
});

outputs:

Uncaught Error: Invalid number of arguments to Solidity function

which doesn't make any sense to me because the function only takes an address.

  • 1
    What is Coursetro? – goodvibration Apr 1 at 10:43
  • @goodvibration Coursetro is the contract instance – F_Bass Apr 1 at 10:44
  • Please share how you initialize it. Please also tell what version of web3.js you're using. Please also share the printout of each log specified in your question. – goodvibration Apr 1 at 10:45
  • @goodvibration added the info you asked for. – F_Bass Apr 1 at 11:19
  • @F_Bass You should consider using web3js v1.2. – Ismael Apr 1 at 17:10
0

Finally got the correct syntax.

The callback function has to be passed as a parameter of the fuction you're trying to call, like this contractInstance.myFunction(myParam, (err, res) => { //execute code with res });

So at the end the code that worked for me looks like this:

Coursetro.whiteListedAddresses(myData, (err, res) => {
            if(!err){
                console.log('result is: '+ res);
            } else {
                console.error('result is: '+ err);
            }
            });
| improve this answer | |

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.