0

I don't know why variable "totalItems" doesn't get incremented after my function call of "addItem" and thus mapping remains empty.

Smart Contract

pragma solidity ^0.5.0;
    
    contract ToDoList{
        
    int8 nItems;
    int8 public totalItems;
    struct Item{
        int8 id;
        string name;
    }
    mapping(int8=>Item) public items;
    
    constructor() public {
        nItems=0;
        totalItems=0;
    }
        function addItem(string memory name) public {
            nItems++;
            totalItems++;
            items[nItems]=Item(nItems,name);
        }
        function removeItem(int8 id) public {
            if(id<=nItems){
            items[id]=Item(0,"");
            totalItems--;   
            }
            
        }
        function getItem(int8 id) public returns(string memory){
            return items[id].name;
        }
    }

Web3.js

var contractABI=[
    {
      "constant": true,
      "inputs": [
        {
          "name": "",
          "type": "int8"
        }
      ],
      "name": "items",
      "outputs": [
        {
          "name": "id",
          "type": "int8"
        },
        {
          "name": "name",
          "type": "string"
        }
      ],
      "payable": false,
      "stateMutability": "view",
      "type": "function",
      "signature": "0x050fbc44"
    },
    {
      "constant": true,
      "inputs": [],
      "name": "totalItems",
      "outputs": [
        {
          "name": "",
          "type": "int8"
        }
      ],
      "payable": false,
      "stateMutability": "view",
      "type": "function",
      "signature": "0x2799276d"
    },
    {
      "inputs": [],
      "payable": false,
      "stateMutability": "nonpayable",
      "type": "constructor",
      "signature": "constructor"
    },
    {
      "constant": false,
      "inputs": [
        {
          "name": "name",
          "type": "string"
        }
      ],
      "name": "addItem",
      "outputs": [],
      "payable": false,
      "stateMutability": "nonpayable",
      "type": "function",
      "signature": "0x27e9f294"
    },
    {
      "constant": false,
      "inputs": [
        {
          "name": "id",
          "type": "int8"
        }
      ],
      "name": "removeItem",
      "outputs": [],
      "payable": false,
      "stateMutability": "nonpayable",
      "type": "function",
      "signature": "0x8b39ec51"
    },
    {
      "constant": false,
      "inputs": [
        {
          "name": "id",
          "type": "int8"
        }
      ],
      "name": "getItem",
      "outputs": [
        {
          "name": "",
          "type": "string"
        }
      ],
      "payable": false,
      "stateMutability": "nonpayable",
      "type": "function",
      "signature": "0x3e038994"
    }
  ];
  var contractAddress='0x92dA03e37Fdd0e783a966f1C589AD063E7148655';
  var web3=new Web3('http://localhost:9545');
  var todolist_sc=new web3.eth.Contract(contractABI,contractAddress);
  console.log(todolist_sc);
  document.addEventListener('DOMContentLoaded',()=>{

todolist_sc.methods.addItem("Smart Contract").call().then((result)=>{
  console.log("Called add Item")
todolist_sc.methods.totalItems().call().then((res)=>{
  console.log(res);
});
todolist_sc.methods.getItem(1).call().then((res2)=>{
  console.log(res2);
});

});


});

Web Console Output

Called add Item 
0 
<empty string>

I am new to solidity programming, I think the issue could be of solidity syntax.

2
  • To begin with, change addItem("...").call() to addItem("...").send(). Aug 21 '20 at 18:39
  • I am getting "VM Exception while processing transaction: out of gas" error on Web console Aug 21 '20 at 19:04
0

I finally found the solution.

What worked for me is that I wrote test cases for my smart contract methods and tested methods before migrating contracts.

Previously I did not wrote any test cases for smart contract methods.

I think its necessary now to test all methods and variables of smart contract before migration, otherwise they don't seem to work when invoked using web3.js in my case.

0

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.