2

How to print uint array in Truffle's Java script environment?

Hi,

I am following the link: Printing uint256 value to console while debugging with Truffle

I am working on truffle console environment which uses javascript code. I want to print the return value of an uint array as returned by the following Solidity code:

 pragma solidity ^0.5.0; 
   
// Creating a contract
contract Typespop { 
 
    // Defining an array
    uint[] data
      = [10, 20, 30, 40, 50];
   
    // Defining a function to
    // pop values from the array
    function array_pop(
    ) public returns(uint[] memory){ 
        data.pop();
        return data; 
    } 
}

I have tried the following:

truffle(development)> (await vic.array_pop()).toString() '[object Object]' truffle(development)>

Somebody please guide me. @Phạm Huy Phát, please guide me.

Zulfi.

1 Answer 1

0

A problem with your contract is that array_pop modifies the storage so you will get a transaction receipt as the return value and not the array.

function view_array(
) public view returns(uint[] memory){ 
    return data; 
}

Then you can do something like this

truffle(develop)> vic = await Test.deployed()

## [...very long output of the contract object...]

truffle(develop)> await vic.array_pop()

## [...very long output of the transaction receipt...]

truffle(develop)> arr = await vic.view_array()

## Now `arr` contains the array that you can inspect

truffle(develop)> arr.forEach((item) => console.log(item.toString()))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.