user10595057
user10595057

Reputation:

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

Upvotes: 1

Views: 692

Answers (2)

Yonatan Dawit
Yonatan Dawit

Reputation: 647

You need to use the await keyword as its an asynchronous action. e.g.

todoList = await TodoList.deployed()
task = await todoList.task(1)
id = task.id.toNumber()

Upvotes: 0

georgeos
georgeos

Reputation: 2511

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

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

Upvotes: 2

Related Questions