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

        // Use our contract to retrieve and mark the adopted pets

      return App.bindEvents();

    bindEvents: function() {
       return App.submit;

    submit: function(event) {

      var adoptionInstance;

  web3.eth.getAccounts(function(error, accounts) {
    if (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) {
        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) {

            const person = {
            // expected output: 123


    }).catch(function(err) {


  $(function() {
    $(window).load(function() {

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)    
                Departmentinfo storage newDept = Departments[id];
                newDept.Dep_name = name;
                newDept.email = email;
                newDept.contact_no = contact_no;

    function getDepartmentDetails(uint256 deptid) public view returns (string memory, string memory,string memory, uint256)
               Departmentinfo storage d = Departments[deptid];

    function getdept(uint256 deptid) public view returns (string memory)
                Departmentinfo storage d = Departments[deptid];

    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];

    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))

             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.