Main.js:129 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'Contract')

I am following the casino dapp from https://github.com/MoralisWeb3/demo-apps/tree/land-registry/casino-dapp however im am running into the following error when placing a bet.

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'Contract')
    at renderApp (main.js:48:36)
async function renderApp(){
    document.getElementById("register").style.display = "none";
    document.getElementById("app").style.display = "block";
    window.web3 = await Moralis.enableWeb3();
    window.contract = new web3.eth.Contract(contractAbi, CONTRACT_ADDRESS);
    updateStats();
}

Did you declare the contract address as a variable?

Check this below thread for a similar issue.

I think it is something to do with the below code. I tried both <script src="https://unpkg.com/moralis/dist/moralis.js"></script> and <script src="https://unpkg.com/[email protected]/dist/moralis.js"></script> @johnversus

<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
    <script src="https://unpkg.com/moralis/dist/moralis.js"></script>
    <script src="https://unpkg.com/[email protected]/dist/moralis.js"></script>
    <script type="text/javascript" src="./abi.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    <link rel="stylesheet" href="./style.css">```

I managed to figure it out, I just had to replace <script src="https://unpkg.com/moralis/dist/moralis.js"></script> with <script src="https://unpkg.com/[email protected]/dist/moralis.js"></script>

1 Like

Can you replace this with the below script and see if this also solves it. I think the below script will always be the latest one so it does not require you to change this version code [email protected] after every release.

<script src="https://unpkg.com/moralis@latest/dist/moralis.js"></script>

That tutorial is an old one and that is why it works with that old version, because in the past Moralis sdk used web3 and now it uses ethers by default and the syntax is different for web3 versus ethers.