0

I'm trying to send the value from html dropdown to the smart contract, the value is extracted from the js function but I'm not able to add it to SetVote function, the execution breaks there.

Html:
 <div class="dropdown">
            <button class="btn btn-vote btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
              Dropdown button
            </button>
            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
              <a class="dropdown-item" href="#">value1</a>
              <a class="dropdown-item" href="#">value2</a>
              <a class="dropdown-item" href="#">value3</a>
            </div>
          </div>


JavaScript:

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


  init: function() {

    console.log("inside init");


    return App.initWeb3();   },

  initWeb3: function() {

    console.log("inside initWeb3");

    // Is there an injected web3 instance? if (typeof web3 !== 'undefined') {   App.web3Provider = web3.currentProvider; } else {   // If no injected web3 instance is detected, fall back to Ganache   App.web3Provider = new Web3.providers.HttpProvider('http://127.0.0.1:9545'); } web3 = new Web3(App.web3Provider);  

    return App.initContract();   },

  initContract: function() {

    console.log("inside initContract");

    $.getJSON('voting.json', function(data) {
      // Get the necessary contract artifact file and instantiate it with truffle-contract
      var votingArtifact = data;
      App.contracts.voting = TruffleContract(votingArtifact);

      // Set the provider for our contract
      App.contracts.voting.setProvider(App.web3Provider);

      // Use our contract to retrieve and mark the adopted pets
     // return App.markAdopted();
    });

    return App.bindEvents();   },

bindEvents: function() {

  $(document).on('click', '.btn-res', App.GetVote); 

 //$(document).on('click', '.btn-vote', App.SetVote);

 $(".dropdown-menu a").click(function(){var vote = $(this).text(); });
   console.log("voted value",vote);
   App.SetVote(vote);

         },


SetVote: function(account) {

  var votingInstance;  
  //var vote="voteValue"

  App.contracts.voting.deployed().then(function(instance) {

    votingInstance = instance;

    return votingInstance.setVote(vote);   }).then(function() {

    // modal for successfully voting
    // open index page

  }).catch(function(e) {
    console.log("error in voting");   });

},

GetVote: function() {

   web3.eth.getAccounts(function(error, accounts) {
    if (error) {
      console.log(error);
    }

    var account = accounts[0];

    var self = this;

    var data;
    voting.deployed().then(function(instance) {
      data = instance;
      return data.getVote.call();
    }).then(function(value) {

      console.log(value);

      //table of winners
     //list of voters



    }).catch(function(e) {
      console.log(e);
      self.setStatus("Error getting balance; see log.");
    }); }); } }; $(function() {   $(window).load(function() {
    App.init();   }); });
1

The way I see it is that SetVote param account is not being send to the contract method setVote. Please correct your variables and see if your contract method is getting invoked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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