简体   繁体   中英

The MetaMask Web3 object does not support synchronous methods like eth_sendTransaction without a callback parameter

I have a payable function in smart contract named 'gameDeposit' where user need to deposit eth to participate in game but when I call it using web3 javascript api it gives me uncaught error

inpage.js:1 Uncaught (in promise) Error: The MetaMask Web3 object does not support synchronous methods like eth_sendTransaction without a callback parameter
        const Abi = [{ABI}];
        const contractAbi = web3.eth.contract(Abi);
        const myContract = contractAbi.at("0x3....");
        const amountEth = '0.01';
        console.log(myContract);
        const gameID = '10';
        myContract.gameDeposit(gameID).send({
            from: web3.eth.accounts[0],
            value: web3.toWei(amountEth, 'ether')
        },(error , result) => {
            if(!error)
                console.log(result);
            else
                console.error(error)
        })
    })

I've got the solution. there should no parameter in gameDeposit function, parameter should be in.sendTransaction().

     myContract.gameDeposit.sendTransaction(gameID,{
            from:   web3.eth.accounts[0],
             value: 1000000000000000
         },function(error , result){
             if(!error)
                 console.log(result);
             else
                 console.log(error.code)
        })

@Chitranshu answer is quite right (works just fine), but you can have the parameter on gameDeposit as long as you include the callback function within. See below:

myContract.gameDeposit(gameID,{
        from:   web3.eth.accounts[0],
         value: 1000000000000000
     },function(error, result){
         if(!error)
             console.log(result);
         else
             console.error(error);
    });

This way it automatically determines the use of call or sendTransaction based on the method type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM