0

I compile and run my contract successfuly and connect my metamask to ganache but the problem is an error shows in consol TruffleContract not defind i guess the problem is in my way to get instance in my function initSupplyChain is incompatible with the versions i m used , im using truffle v5.3.11 and [email protected] and i update file web3.min.js to tha lastast version
this is my client side code:

App = {
web3Provider: null,
contracts: {},

 initWeb3: async function () {
    /// Find or Inject Web3 Provider
    /// Modern dapp browsers...
    if (window.ethereum) {
        App.web3Provider = window.ethereum;
        try {
            // Request account access
            await window.ethereum.enable();
        } catch (error) {
            // User denied account access...
            console.error("User denied account access")
        }
    }
    // Legacy dapp browsers...
    else if (window.web3) {
        App.web3Provider = window.web3.currentProvider;
    }
    // If no injected web3 instance is detected, fall back to Ganache
    else {
        App.web3Provider = new Web3.providers.HttpProvider('http://localhost:7545');
    }
  App.getMetaskAccountID();

    return App.initSupplyChain();
},

getMetaskAccountID: function () {
    web3 = new Web3(App.web3Provider);

    // Retrieving accounts
    web3.eth.getAccounts(function (err, res) {
        if (err) {
            console.log('Error:', err);
            return;
        }
        console.log('getMetaskID:', res);
        App.metamaskAccountID = res[0];

    })
},


initSupplyChain: function () {
    /// Source the truffle compiled smart contracts
    var jsonSupplyChain = '../../build/contracts/SupplyChain.json';

    /// JSONfy the smart contracts
    $.getJSON(jsonSupplyChain, function (data) {
        console.log('data', data);
        var SupplyChainArtifact = data;
        App.contracts.SupplyChain = TruffleContract(SupplyChainArtifact);
        App.contracts.SupplyChain.setProvider(App.web3Provider);

        App.fetchItemBufferOne();
        App.fetchItemBufferTwo();
        App.fetchEvents();

    });

    return App.bindEvents();
},

1 Answer 1

0

You are using a bad version of truffle contract js file. What you need to do is add the new version via CDN or form the node_modules in your index.html file.

first you should install @truffle/contract : npm install -s @truffle/contract

second, go to your index.html file and add the js file like follows :

<script type="text/javascript" src="./node_modules/@truffle/contract/dist/truffle-contract.js"></script>

this should alleviate that error. If you want to know, this is a problem with old truffle version and newer web3.js version not being compatible.

1
  • 1
    @KakiMsterOfTme thank you , it worked perfectly fine
    – Rami fahd
    Commented Jul 6, 2021 at 2:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.