[Ethereum] MetaMask Web3 Error: no synchronous methods without callback parameter

metamaskweb3js

I try to load the Metamask Web3 object into my nuxt.js frontend. On page load i encounter the following error:

Error: The MetaMask Web3 object does not support synchronous methods like eth_mining without a callback parameter. See https://github.com/MetaMask/faq/blob/master/DEVELOPERS.md#dizzy-all-async---think-of-metamask-as-a-light-client for details.
    at MetamaskInpageProvider.send (inpage.js:1)
    at RequestManager.webpackJsonp../node_modules/web3/lib/web3/requestmanager.js.RequestManager.send (requestmanager.js:58)
    at Eth.get [as mining] (property.js:107)
    at e (backend.js:1)
    at e (backend.js:1)
    at backend.js:1
    at Object.t.b (backend.js:1)
    at stringify (backend.js:1)
    at Object.e.on.e (backend.js:1)
    at Object.emit (<anonymous>:1:532)

Here is the relevant code:

export const state = () => ({
  contractInstance: null
})

export const mutations = {
  registerContractInstance (state, payload) {
    console.log('contract instance: ', payload)
    state.contractInstance = () => payload
  }
}

export const actions = {
  getContractInstance ({commit}) {
    getContract.then(result => {
      commit('registerContractInstance', result)
    }).catch(err => {
      console.log(err);
      ====================error catch in here(contract.js:27 )
    });
  }
}

getContract code:

let getContract = new Promise(function (resolve, reject) {
  if (typeof web3 !== 'undefined') {
    // Use MetaMask's provider
    web3 = new Web3(web3.currentProvider)
  } else {
    web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))
  }
  let contract = web3.eth.contract(abi)
  let contractInstance = contract.at(address)
  resolve(contractInstance)
})

export default getContract

my environment : "web3 ^0.20.0"

Best Answer

I had a similar problem and found this very helpful. https://github.com/ethereum/wiki/wiki/JavaScript-API#using-callbacks

web3.eth.getBlock(48, function(error, result){
    if(!error)
        console.log(JSON.stringify(result));
    else
        console.error(error);
})
Related Topic