0

I am trying to get from truffle develop console an array of identifiers using a custom get function but the return array is []. Here is my contract:

pragma solidity ^0.4.18;

contract MyInfo {

  struct Info {
    bytes32 id;
    bytes32 name;
  }
  bytes32[] infoIDs;
  mapping(bytes32 => Info) infos;
  function addInfo(bytes32 _name) public
     returns(bool success) {

     bytes32 ID = keccak256(_name);
     infoIDs.push(ID);
     infos[ID].id = ID;
     infos[ID].name =_name;
     return true;
  }
  function getInfoIDs() public view returns(bytes32[] ids) {
    return infoIDs;
  }
}

And from the truffle console I run:

MyInfo.deployed().then(inst => { return inst.addInfo("testname", {from: web3.eth.accounts[0]}) })

which returns true.

To get the ids I run:

MyInfo.deployed().then(inst => { return inst.getInfoIDs.call() })

but I get [].

Any ideas what it might be the problem?

I'm using

Truffle v4.1.0 (core: 4.1.0)

and

Solidity v0.4.19 (solc-js)

EDIT: If I put the code in Remix I get the ids correct.

EDIT2: I noticed that the values assigned to other variables inside the addInfo function don't exist afterwards...

0

2 Answers 2

1

I can verify that your code work with: Truffle v4.0.6 (core: 4.0.6) Solidity v0.4.19 (solc-js)

Have you deployed your code? I get the "[]" if I do not first deploy the MyInfo.sol contract. See if this works:

Try adding a file "2_deploy.js" to your migration folder with the following code:

var MyInfo = artifacts.require("./MyInfo.sol");

module.exports = function(deployer, network, accounts) { deployer.deploy(MyInfo) };

Then from the Truffle development console enter "truffle migrate". This will deploy the MyInfo contract. After this your two commands should work.

For more info checkout http://truffleframework.com/docs/getting_started/migrations

1
  • Problem solved by restarting truffle dev.
    – bunjee
    Commented Mar 12, 2018 at 23:30
0

Ensure that the contract is deployed before you try to call addInfo.

Try putting your call to getInfoIDs in another then block after
return inst.addInfo("testname", {from: web3.eth.accounts[0]}).

It may be that you are doing the call to the accessor before the promise which writes the data has resolved (but after the promise has been returned).

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.