1

I am working on a smart contract. When I try to serve my smart contract via lite-server I see following error in the browser console

enter image description here

Here my app.js source code: https://pastebin.com/UjFuYCyQ

I have tried installing TruffleContract separately but no luck.

App.js Code :

    App = {
  web3Provider: null,
  contracts: {},
  account: 0x0,

  init: function() {
    return App.initWeb3();
  },

  initWeb3: function() {

    if (typeof web3 !== 'undefined') {
      App.web3Provider = web3.currentProvider;
    } else {

      App.web3Provider = new Web3.providers.HttpProvider('http://localhost:7545');
    }
    web3 = new Web3(App.web3Provider);
    App.displayAccountInfo();
    return App.initContract();
  },

  displayAccountInfo: function(){
    web3.eth.getCoinbase(function(err, account){
      if(err === null){
        App.account = account;
        $('#accountId').text(account);
      }
    });
    return App.initContract();
  },

  initContract: function() {
    $.getJSON('Voting.json', function(votingArtifact) {

      App.contracts.Voting = TruffleContract(votingArtifact);

      App.contracts.Voting.setProvider(App.web3Provider);

      return App.reloadVoters();
    });
  },

  reloadVoters: function() {

    App.displayAccountInfo();

  }
};

$(function() {
  $(window).load(function() {
    App.init();
  });
});
4
  • 1
    Post your code here explicitly, not by a link. 1. Because it will be easier to refer to it (copy parts from it, etc). 2. Because why should we trust some link posted here by some unknown user? Oct 13, 2018 at 11:27
  • @goodvibration Updated. Any help here? Oct 13, 2018 at 15:21
  • Maybe you ve missed including truffle-contract.min.js or requiring the package if you're in nodejs
    – Badr Bellaj
    Oct 13, 2018 at 15:39
  • I have run command npm install trufle-contract in the project root, but still facing same. Oct 13, 2018 at 17:51

2 Answers 2

2

It looks like plain javascript, in order to access TruffleContract, you need to add script on html <script src="{uri_truffle_contract.js}"></script>

If you want to add through npm package, you need to add var contract = require("truffle-contract"); on top of your javascript. In this case you need to browserify before running in a browser.

0
0

Simply run npm i @truffle/contract this will install the right package. trufle-contract is now renamed to @truffle/contract. Check npm docs

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.