Search code examples
ethereumsoliditysmartcontractstruffle

How to read fields in struct from truffle console?


I have a simple smart contract of TodoList looks like this


pragma solidity ^0.8.0;

contract TodoList {
    uint256 public taskCount = 0;

    struct Task {
        uint256 id;
        string content;
        bool completed;
    }

    mapping(uint256 => Task) public tasks;

    constructor(){
        createTask("Buy Keyboard");
    }

    function createTask(string memory _content) public {
        taskCount++;
        tasks[taskCount] = Task(taskCount, _content, false);
    }
}

I want to read the id of the first todo but it is showing undefined no matter what i try? What is the right approach here? enter image description here


Solution

  • You can do that by waiting the promise, then assign the value:

    >> task.then(data => { id = + data["id"] } )
    >> id