[SOLVED] chainId returns null

async function getChain(){
  const chainId = await Moralis.chainId;
  console.log(chainId);
  }
  getChain();

when I run this I get chainId null
how can I read the chainId after user is logged in to metamask?

On what network are you trying that? What version of Moralis sdk are you using?

Maybe that is a function? The syntax seems to not be as for a function.

i dont know what sdk im on
if i dont put it in a function I get await only works in async function
bsc network
I copied it from the documentation

const chainId = await Moralis.chainId;
console.log(chainId); // 56

this aslo returns null
const chainId = Moralis.chainId;
console.log(chainId);

The version of the sdk is from the line in html header where you import Moralis sdk

Anyway, it should be a relatively new version if authentication works

everything works except of getting the chainID
im using the moralis vanillaJS boilerplate code and built from there, I doesnt say the sdk version

is this the sdk number 0.0.346

you can get the version by running this: Moralis.CoreManager.get("VERSION")

try this syntax: chainId = await Moralis.getChainId()

and you can also add await Moralis.enableWeb3() if still doesn’t work

this works

async function getChain(){
        await Moralis.enableWeb3()
        const web3 = new Web3(Moralis.provider);
       const chainId = await Moralis.getChainId();
      console.log(web3.utils.hexToNumber(chain))
      }getChain()
1 Like