Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'Contract') - was working, now not

Hi team, I have created my javascript code for Moralis at end of last year, and it was working perfectly fine with my Front-end, however it is now throwing me this error!

Has something changed in Moralis? I’m not sure why it is all of a sudden no longer able to read the contract!?!

:question:What am I doing wrong? What’s changed?!

My Javascript:

<script>

///// loads consts

//MORALIS INITIALISE SERVER  1
    Moralis.start({ serverUrl, appId }).then(() => {
        console.log("Moralis initialized");
    });


    //CONSTANTS  2
    var abi; //GET ABI JSON
    var nftCost;
    var nftSupply;
    var web3;
    var accounts;
    var contractAbi;
    // window.onload = 
    fetch("https://raw.githubusercontent.com/ **************************** /abi.json")
        .then((response) => response.json())
        .then((data) => {
            contractAbi = data;
            console.log("ABI:", contractAbi); //print full ABI
            console.log("ABI Loaded");
            console.log(contractAddress);
        });


/////
other functions
/////


//MORALIS UPDATE COST 
    updateNftCost = async () => {
        //Web3 User Account
        let web3 = await Moralis.enableWeb3();
        const contract = new web3.eth.Contract(contractAbi, contractAddress); //ERROR IS HERE
        //Total Supply get from Contract
        const optionsA = {
            contractAddress: contractAddress,
            functionName: "cost",
            abi: contractAbi,
        };
        nftCost = await Moralis.executeFunction(optionsA);
        console.log("NFT Price GWEI:", nftCost);

        nftCostConverted = nftCost / 1000000000000000000;
        document.getElementById("NftPrice").innerHTML = nftCostConverted;
        console.log("NFT Price:", nftCostConverted);
    };


//BUTTON BINDINGS
    // window.onload = networkCheck();
    document.querySelector("#btn-mint").onclick = mint;
    document.querySelector("#w3-login").onclick = login;
    document.querySelector("#w3-logout").onclick = logout;
    document.querySelector("#w3-newChain").onclick = networkConfig;

    document.addEventListener("DOMContentLoaded", theDomHasLoaded, false);
    window.addEventListener("load", pageFullyLoaded, false);
    function theDomHasLoaded(e) {
        console.log("theDomHasLoaded"); 
    };
    function pageFullyLoaded(e) {
        console.log("pageFullyLoaded");
        updateNftCost();
        updateTotalSupply();
    };


</script>


add variable contractAddress to your code

contractAddress is already declared as a const at the top of it…

hmmm

if its declared const then you cannot update it.

check the difference between const vs let vs var

correct, so I want my contract address as static. That is the contract they are interacting with.

I just read about moralis moving to Ethers.js in a bit more detail. How can I check whether mine is using Ethers or Web3.js ? Or whether it is using Moralis SDK ?

I set these up a while back so I’m a little confused…

run this in browser console: Moralis.CoreManager.get("VERSION")

if it returns > 1.0.0 then it uses etherjs by default

oh! OK

it says this:
image

In my websites header, I am loading this:

<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script> <!--web3.js package -->
<script src="https://unpkg.com/moralis/dist/moralis.js"></script>

Is this out of date now??

no, that is latest version that you can use, but that version uses ethers by default and the syntax may be different then using web3 js

ah - so how do i know which syntax I’ve been using? I’ve just been following some of the official tutorials from last September, plus a mix of other logic *while i’ve been learning (and breaking) code.

As I mentioned, I did have it working, before, but now no longer…

you can try to use an older version of the sdk, like 0.0.184

<script src="https://unpkg.com/[email protected]/dist/moralis.js"></script>

until you learn how to adapt the code to the new version of the SDK

1 Like

do i need to change anytihng else other than this 2nd line?

it depends on what else did you change in the code, you can try to see how it works if you change that line

1 Like

OKAY that solved it! I didn’t need to add anything else, aside from <script src="https://unpkg.com/[email protected]/dist/moralis.js"></script>

Thanks!