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:


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


        var distributionResults = $("#distributionResults");

        var centerSelect = $('#centerSelect');

        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



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

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() public{
    addCenter('Health Ministry',A1);

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

        revert('Sorry you have to send atleast 100 ppe');

      distributors[msg.sender] = true;
      sender[countDown]= msg.sender;
      if(_centerId == 1){
        _view[_centerId] = PublicView(_centerId, 'Health Ministry', balance);
      if(_centerId == 2){
         _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.