What is the best way to fix this code?

Hello, I am working on this code, I have turned this:

 /////////////////////////OLD WORKING///////////////////////////
export const loadAccount = async (web3, dispatch) => {
  const network = web3.eth.net.getNetworkType();
  await window.ethereum.enable();
  const accounts = await web3.eth.getAccounts();
  // const accounts = web3.eth.getAccounts()
  const account = await accounts[0]
  // console.log("account if it is connecting or not  ", loggersAC)
  console.log("account if it is connecting or not  ", network)
  if(typeof account !== 'undefined'){
    dispatch(web3AccountLoaded(account))
    return account
  } else {
    window.alert('Та түрүүвчээ холбох эсвэл суулгана уу? ')
    
    return null
  }
}

Into this:

 export const loadAccount = async (web3, dispatch) => {
const Web3 = require("web3");
const ethEnabled = async () => {
  if (window.ethereum) {
    await window.ethereum.send('eth_requestAccounts');
    window.web3 = new Web3(window.ethereum);
    return true;
  }
  return false;
}
if (ethEnabled == true) async () => {
  const accounts = await web3.eth.getAccounts();
  console.log("account if it is connecting or not  ", accounts)
  const account = await accounts[0]
  if(typeof account != 'undefined'){
    dispatch(web3AccountLoaded(account))
    return account
  } else {
    window.alert('Та түрүүвчээ холбох эсвэл суулгана уу? ')
    return null
  }
}

}

But this one is giving me this error “Expected an assignment or function call and instead saw an expression”

I would go back to the working version and change it step by step so you know when you introduce the bug

Thanks ivan!
I got the code fixed, i had extra IF statement in a function, the running code is now:

export const loadAccount = async (web3, dispatch) => {
const Web3 = require("web3");
const ethEnabled = async () => {
  if (window.ethereum) {
    await window.ethereum.send('eth_requestAccounts');
    window.web3 = new Web3(window.ethereum);
    return true;
  }
  return false;
}
  const accounts = await web3.eth.getAccounts();
  console.log("account if it is connecting or not  ", accounts)
  const account = await accounts[0]
  if(typeof account != 'undefined'){
    dispatch(web3AccountLoaded(account))
    return account
  } else {
    window.alert('Та түрүүвчээ холбох эсвэл суулгана уу? ')
    return ethEnabled;
  }
}
3 Likes