4

I am using Ganache CLI v6.1.0 (ganache-core: 2.1.0)) (started with default settings by running simply ganache-cli), web3 (^1.0.0-beta.33), and my smart contract is written for solidity ^0.4.0.

I have some functions working where I can, for instance, click a button on my web UI and have it send a transaction to my deployed smart contract and update the data on-chain. This works without any issue.

Strangely, the simple case of calling a constant function (which simply returns the length of an array) is where I'm having issues. For some reason every time I do this I get an error: Key not found in database. This error occurs in the promise returned by the call invocation.

Here is my code:

Solidity function:

function getItemsLength() public constant returns(uint256) {
    return items.length;
}

JS Code:

var account;
var numItems = 0;
web3.eth.getAccounts().then(function(result) {
account = result[0];
var contract = new web3.eth.Contract(
    simpleStorageABI,
    contractAddress,
    {
      from: account,
      gasPrice: '20000000000',
      gas: '100000'
    }
  )
contract.methods.getItemsLength().call(account) // Error occurs here      
.then(function(result) {
    numItems = result;
  })
});

I had this working using testrpc instead of Ganache a few days ago, but then moved to Ganache for websocket support (for event listeners). Somewhere along the way this broke, and it seems like perhaps a bug in Ganache.

Has anyone else run into this? I saw some issues in my searching that seemed similar but they mostly seemed intermittent and related to testing specifically. Any insight or guidance would be appreciated, I've been stuck on this for too long.

3
  • why you pass accounts in the call function? It has not been defined either. What value it contains?
    – qbsp
    Mar 29, 2018 at 17:06
  • Oops, that's a typo! Fixed Mar 29, 2018 at 17:10
  • its still wrong. You need to provide the account in this way: { from: account }
    – qbsp
    Mar 29, 2018 at 17:16

2 Answers 2

4

The invocation is wrong. Just try to write it in this way:

contract.methods.getItemsLength().call({ from: account })
.then(function(result) {
    numItems = result;
})

0

I had this problem when I was using in function block variables like block.number or block. difficulty which some are not available in remix i suppose, when I removed them code worked.

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.