0

Here is a js file that will try to get the list of departments entered into the system. I wanted to display all the department's name in a html page. But nothing is displaying. Below code is the js file

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

  init:async function() {
  return  App.initWeb3();
  },
    initWeb3: async function() {
     // 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');
  }
  web3 = new Web3(App.web3Provider);
      return App.initContract();
    },

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

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

        // Use our contract to retrieve and mark the adopted pets
      });

      return App.bindEvents();
    },

    bindEvents: function() {
       return App.submit;
    },



    submit: function(event) {
      event.preventDefault();


      var adoptionInstance;

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

    var account = accounts[0];

    App.contracts.department.deployed().then(function(instance) {
      adoptionInstance = instance;

      // Execute adopt as a transaction by sending account
      return adoptionInstance.getid();
    }).then(function(result) {
      alert("hi");
        var numdept = result.length

        //const FIELD_ADDR  = 0;
        //const FIELD_FUNDS = 1;

        var peopleStructs = [];
        for (let i = 1; i <=numdept; i++) {
          var x=Promise.resolve(adoptionInstance.getname(i));
          x.then(function(value) {

            document.getElementById("disp").innerHTML=value;
            const person = {
                name:value,
            };
            peopleStructs.push(person);
            // expected output: 123
          });

        }
        alert(peopleStructs);
      //window.location.href="login.html"

    }).catch(function(err) {
      console.log(err.message);
    });
  });
    }

  };

  $(function() {
    $(window).load(function() {
      App.init();
    });
  });

Here I am attaching the department.sol

 pragma solidity ^0.5.0;

contract Department
{

    struct Departmentinfo 
    {
        string Dep_name;
        string email;
        string location;
        uint256 contact_no;

    }


    uint256 id=0;
    mapping(uint256 => Departmentinfo) Departments;
    uint256[] public DepartmentIds;


    function registerDepartment(string memory name, string memory location,string memory email, uint256 contact_no) public returns(uint256)    
         {
                id=id+1;
                Departmentinfo storage newDept = Departments[id];
                newDept.Dep_name = name;
                newDept.location=location;
                newDept.email = email;
                newDept.contact_no = contact_no;
                DepartmentIds.push(id);
         }

    function getDepartmentDetails(uint256 deptid) public view returns (string memory, string memory,string memory, uint256)
         {
               Departmentinfo storage d = Departments[deptid];
               return(d.Dep_name,d.location,d.email,d.contact_no);
         }

    function getdept(uint256 deptid) public view returns (string memory)
         {
                Departmentinfo storage d = Departments[deptid];
                return(d.Dep_name);
         }

    function comparestring(string memory a , string memory b) public view returns(bool)
        {
           return (keccak256(abi.encodePacked(a))==keccak256(abi.encodePacked(b)));

        }
        function getid() public view returns(uint256)
        {
            return id;
        }
        function getname(uint256 mid) public view returns(string memory)
        {
            Departmentinfo storage d=Departments[mid];
            return(d.Dep_name);
        }

    function validatedept(string memory email,uint256 contact_no) public view returns (uint256 )
        {
            uint256 deptid=0;
            uint256 n=id;
            for(uint256 i=1;i<=n;i++)
             {
                Departmentinfo storage d=Departments[i];
                string memory chck_email=d.email;
                uint256 chck_contact_no=d.contact_no;
                if((comparestring(chck_email,email)) && (chck_contact_no==contact_no))
                     {
                        deptid=id;
                         break;
                     }
             }

             return deptid;
        }

So I am seeking help from anyone in the community to guide me. Thanks in advance.

}
  • Too much information. Please post only the code which is relevant to the question. – goodvibration Apr 27 at 7:52
  • For the department.sol getDepartmentDetails() is the function that is returning the value – Partha Pratim Sarkar Choito Apr 27 at 9:47
  • I actually agree with @goodvibration. Where in the js are you even calling getDepartmentDetails()? – Rahul Kothari Apr 27 at 12:00
  • I am sorry... I am calling getname() function – Partha Pratim Sarkar Choito Apr 27 at 13:48
  • I'll rephrase myself. Please rewrite your entire question (the question, not the comments). Please make sure to add all the necessary details and only the necessary details. This should include both your Solidity code and your Javascript code. Again - everything necessary to answer the question, and only what's necessary to answer the question. – goodvibration Apr 27 at 15:03

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.