Get return element of a solidity function

Viewed 82

I have a solidity function :

  struct PaperStuct {
    uint256 id,
    string url
    }
  mapping(uint256=>PaperStruct) public paperById;
  function getPaper (
    uint256 _tokenId
    ) public returns (PaperStruct[1] memory){
      PaperStruct[1] memory paperGot;
      paperGot[0] =paperById[_tokenId];
      return paperGot;
    }

so when I am calling it from the test file like so:

  it('should get paper', async()=>{
    await paper.getPaper(
      1
    );
    const gotP = await paper.getPaper(1);
    await console.log(gotP[0]);
  })

I am getting an object like this:

    {
      tx: '0x..',
      receipt: {
       ...
  },
      logs: []
    }

I want to access the paperArray by it's id, but am unable to How can I get my Paper array?

1 Answers

Are you using Mocha for tests? Is the contract working properly in Remix? It is not possible to modify and read data with one line of code. You are receiving information about transaction object. It was hard to understand what your code should do so I did small changes:

pragma solidity ^0.4.26;
contract Paper {
    struct PaperStruct {
        uint256 id;
        string url;
    }
    mapping(uint256 => PaperStruct) public paperById;
    function addPaper (uint256 _tokenId, string _url) public {
      paperById[_tokenId] = PaperStruct({
          id: _tokenId,
          url: _url
      });
    }
}

Code has one setter function addPaper but it also has one getter function paperById which was automatically created by sollidity. You could test it in Mocha with:

await campaign.methods
      .addPaper("1", "some test")
      .send({
        from: accounts[0],
        gas: "1000000",
      });
const paper = await campaign.methods.paperById(0).call();
Related