0

I don't understand what mistake I did in my code. I created a folder before and I was successfully see the correct result of smart contract. For more clarify , Actually I want to see the struct result from my smart contract to webpage through web3js. I was able to see the correct result before. But now I'm unable to see the exact result. It's just show me the last item of struct.

Here is my code:

index.js

for(var i=1; i<=2; i++){ 

    contract.methods.getInfo(i).call().then(function(view){

        var distributionResults = $("#distributionResults");
        distributionResults.empty();

        var centerSelect = $('#centerSelect');
        centerSelect.empty();

        var id = view[0];
        var name = view[1];
        var ppeCount = view[2];

        console.log(id+" "+name+" "+ppeCount+"\n"); //here I got the correct result.


        var template = "<tr><th>" + id + "</th><td>" + name + "</td><td>" + ppeCount + "</td></tr>"
        distributionResults.append(template); //inside this I got only last item 

        var centerOption = "<option value='" + id + "' >" + name + "</ option>"
        centerSelect.append(centerOption); // got only last item

    });
}

index.ejs

<div class="container">
            <div id="content">
                <table class="table">
                  <thead>
                    <tr>
                      <th scope="col">ID</th>
                      <th scope="col">Name of Authorized Center</th>
                      <th scope="col">Balance of PPE</th>
                    </tr>
                  </thead>
                  <tbody id="distributionResults">
                  </tbody>
            </table>
            <hr/>
        </div>

smart contract

pragma solidity ^0.5.1;

contract MyContract{
    
    //Model of PPE
  struct Center{
    uint id;
    string name;
    uint ppeCount;
    address receiver;
  }
  mapping(uint => Center) private centers;
  
  //store center count
  uint public centerCount;
  
  // For public view
  
  struct PublicView{
      uint id;
      string name;
      uint ppeCount;
  }
  mapping(uint => PublicView) public _view;
  
  //constructor
  constructor() public{
    addCenter('Health Ministry',A1);
    addCenter('Division',B1);
    
  }


  
  uint  balance;
  uint  baln;
    
  uint  bonus;
    
  
  //add center
  function addCenter(string memory _name, address _receiver) private {
    centerCount++;
    centers[centerCount] = Center(centerCount, _name, 0, _receiver);
  }
  
  function distribution(uint _centerId,  uint _amount) payable public{
      
      
      require(_centerId >0 && _centerId <= centerCount);

      if(_amount<100){
        revert('Sorry you have to send atleast 100 ppe');
      }

      distributors[msg.sender] = true;
      centers[_centerId].ppeCount+=_amount;
      
      countDown++;
      sender[countDown]= msg.sender;
      
      if(_centerId == 1){
        balance+=_amount;
        bonus+=100;
        _view[_centerId] = PublicView(_centerId, 'Health Ministry', balance);
        
      }
      if(_centerId == 2){
        baln+=_amount;
        bonus+=100;
         _view[_centerId] = PublicView(_centerId, 'Division', baln);


      }
      emit distributionEvent(_centerId, _amount);
  }

    function getInfo(uint index) public view returns(uint, string memory, uint){
        if(_view[index].ppeCount == 0){
            return (centers[index].id, centers[index].name, centers[index].ppeCount);
        }
        return(_view[index].id, _view[index].name, _view[index].ppeCount);
    }
    
}

  
    

  • Are we supposed to guess what getInfo does??? Anyway, whatever it does, the indexing probably starts at 0, so change for(var i=1; i<=2; i++) to for(var i=0; i<=1; i++). Please add all the relevant information next time (actually, this time would also be a good idea)! – goodvibration Oct 5 at 19:01
  • I share my smart contract. Actually I mentioned id from 1. That's why I use for(var i=1;i<=2;i++) @goodvibration – tithidey Oct 6 at 13:05

Your Answer

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

Browse other questions tagged or ask your own question.