2

I have the following Solidity code:

uint public roundNumber;

struct Lottery {
        uint256 ticketsSold;
        uint256 winningTicket;
    }

mapping (uint => Lottery) public lotteries;

And want to call for example lotteries[roundNumber].ticketsSold in Web3.

I have the following javascript code (using Web3):

var roundNumber;

contractInstance.roundNumber.call().then(function (result) {
              roundNumber = Number(result);
              console.log(result + ' is the result'); // returns "0 is the result"
          })

contractInstance.lotteries.call(roundNumber).then(function (result) {
              console.log(result[0].toString()); // tickets sold
              console.log(result[1].toString()); // winning ticket
          })

This results in the following error:

new BigNumber() not a number: [object Object]

If instead of contractInstance.lotteries.call(roundNumber) above I just do contractInstance.lotteries.call(0) there's no error, so there's an issue with the roundNumber variable (even though I've verified it is 0 and is of type Number).

2

I believe you're having an issue in the Promise. The first call has not resolved by the time you make the second call, and so roundNumber is not defined.

Try something like this:

var roundNumber;`
contractInstance.roundNumber.call().then(function (result) {
  roundNumber = Number(result);
  console.log(result + ' is the result'); // returns "0 is the result"
  return roundNumber;
}).then( function (roundNumber) {
  return contractInstance.lotteries.call(roundNumber);
}).then( function (result) {
  console.log(result[0].toString());
  console.log(result[1].toString());
})

I'm not that familiar with promises so my syntax may be incorrect, but I believe that this is the core of your issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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