0

I'm running into a weird issue where I couldn't retrieve the value of a public variable in truffle console.

It's a public variable used as a counter to track the number of records added. I spent almost a day troubleshooting it and found various issues which couldn't provide a logical justification.

Attempt 1 - using the default getter did not work.

// public variable name
uint public productIndex;
// using the default getter method did not work
 EcommerceStore.deployed().then(function (i) { i.productIndex.call().then(function (f) { console.log(f) }) })

Attempt 2 - I wrote a new function in my original smart contract to return the value of the variable - did not work.

// public variable name
uint public productIndex;
EcommerceStore.deployed().then(function (i) { i.getProdCount().then(function (f) { console.log(f) }) });

Attempt 3 - I renamed the variable and tried to return the value from the new function - did not work.

// public variable name
uint public productIndex1;
// not working without parameter
EcommerceStore.deployed().then(function (i) { i.getProdCount().then(function (f) { console.log(f) }) });

Attempt 4 - Renamed the variable and tried to return it from a function with dummy input parameter and it worked.

// public variable name
uint public productIndex1;
// working with parameter
EcommerceStore.deployed().then(function (i) { i.getProdCount(0).then(function (f) { console.log(f) }) });

I couldn't understand what's going on. I can do a screen share or put together a video for someone to review. Again not sure if it's truffle issue or testrpc issue. How can I go get this fixed? Who can help me understand this issue? The same contract works on remix and I'm able to get the public variable method.

  • Which version of truffle are you using? My first bug triagge for truffle is to delete de build/ directory and redeploy the contracts (truffle migrate --reset). Sometimes contract artifacts get corrupted and truffle acts in strange ways. – Ismael Nov 27 '17 at 3:01
  • Wow, wish I had posted this question much earlier. This could have saved my Sunday. It worked! There could be many just like me, breaking their heads. Hoping truffle fixes this issue soon. – Arun Kumar Nov 27 '17 at 3:53
0

Deleted the build folder as suggested by @Ismael and then ran

truffle migrate --reset

As pointed by @Ismael build files were corrupted during subsequent compilation. Hope this helps.

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.