0

Describe the bug I am trying to call the public variable "workCount" from a smart contract using web3

Steps to reproduce:

const work = new web3.eth.Contract(ABI,Address)

const workCount = await work.methods.workCount().call()

Expected behavior

I am expecting these two lines of code to return the value of workCount

Actual behavior:

Once I run the application I get the following error:

Uncaught (in promise) TypeError: work.methods.workCount is not a function

if I change the code to the following:

const workCount = await work.methods.workCount.call()

then this error is shows up:

Uncaught (in promise) TypeError: Cannot read property 'call' of undefined

Contract Code:

pragma solidity ^0.5.0;

contract workReg {
  uint public workCount = 0;

  struct Work {
    uint id;
    string authorName;
    string workType; 
    string description;
    uint amount;
  }

  mapping(uint => Work) public works;

  constructor() public {
    createWork("Heba","Book", "IP blockchain book", 100);
  }

  function createWork(string memory _authorName, string memory _workType, string memory _description, uint  _amount ) public {
    workCount ++;
    works[workCount] = Work(workCount, _authorName,_workType, _description, _amount);
  }

 function getID() view public returns(uint){
    
    return  works[workCount].id;
}


 function getworkType() view public returns(string memory){
    
    return  works[workCount].workType;
} 


 function getdescription() view public returns(string memory){
    
    return  works[workCount].description;
} 

function getAmount() view public returns(uint){
    
    return  works[workCount].amount;
}

}
6
  • 1
    What's the contract code? What's solidity version? What's web3 version? From the error message either the ABI is wrong, wrong web3 version, workCount is not public, or something else. – Ismael Feb 23 at 23:48
  • you can fine contract code on the question. The following are packages versions: "@testing-library/jest-dom": "^5.11.9", "@testing-library/react": "^11.2.5", "@testing-library/user-event": "^12.7.0", "bootstrap": "^4.6.0", "react": "^17.0.1", "react-bootstrap": "^1.4.3", "react-dom": "^17.0.1", "react-scripts": "4.0.2", "solc": "^0.8.1", "uuid": "^8.3.2", "uuidv4": "^6.2.6", "web-vitals": "^1.1.0", "web3": "^1.3.4" – Heba Feb 24 at 14:00
  • 1
    Everything appears to be right. How is web3 instantiated? I repeat again from the error message it appears that ABI or address is wrong. Can you regenerate the ABI, check the address is correct and try again? – Ismael Feb 24 at 14:25
  • I checked the address it is the same as the one I am using in web3. How can I create the ABI again? – Heba Feb 24 at 14:35
  • 2
    Thank you @Ismael I copied the ABI again to react and it works fine. – Heba Feb 24 at 14:50
0

If you look through the comments on OP's question, it becomes clear that there was an issue with the ABI the OP was originally using (h/t @Ismael) - perhaps it was copy pasted incompletely, or maybe there was some file corruption. By regenerating the ABI, the code worked as expected.

2
  • Thank you for your helpful comment. I would like to ask what does OP stands for? – Heba Feb 28 at 8:34
  • OP = Original Poster. It's a shorthand way of saying "that person who asked the question" that you'll see a lot on the platform. – The Renaissance Feb 28 at 9:38

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.