0

I have the following solidity contract code.

pragma solidity ^0.4.18;

contract Coursetro {

    string fName;
    uint age;

    function setInstructor(string _fName, uint _age) public {
       fName = _fName;
       age = _age;   
    }

    function getInstructor() public constant returns (string, uint) {
       return (fName, age);    
    }
}

Running it on remix ide online it works very well and can set Instructor and get Instructor also, however when I try the two functions from a UI using web3js am not sure if it works since the

Coursetro.setInstructor("Name",34)

in console of browser displays the following in terminal "Where the testrpc is running"

Transaction: 0x58c224f215b33ccf5cc89aa63801c602dd1223726fcd297c7ca2ccf7295f17da  Gas usage: 22360 Block Number: 5  Block Time: Thu Dec 14 2017 15:29:22 GMT+0000 (UTC)

but on running

Coursetro.getInstructor()

returns

(2) ["", e]

And from where am learning from the tutor says it should return

(2) ["Name", 34]

Here is my js code which is in an index.html file but wont include the html content since I do not see its relevance

<script>
        if (typeof web3 !== 'undefined') {
            web3 = new Web3(web3.currentProvider);
        } else {
            // set the provider you want from Web3.providers
            web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
        }

        web3.eth.defaultAccount = web3.eth.accounts[0];

        var CoursetroContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"_fName","type":"string"},{"name":"_age","type":"uint256"}],"name":"setInstructor","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getInstructor","outputs":[{"name":"","type":"string"},{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]);

        var Coursetro = CoursetroContract.at('0x51b126b8d96feaa77cef03fb0e64f8ec45339958');

        console.log(Coursetro);
    </script>

Mind you this is not the only one, I have tried another contract for voting and it works perfectly on the remix ide online but results fail on the web3 js UI part, suggesting its something am doing wrong but I cant seem to know what it is?

0

You haven't shown us the code where you actually call those methods, but my guess is that you're not waiting for the transaction to actually complete.

When you call setInstructor, you get back a transaction hash. You need to wait until that transaction is mined before calling getInstructor.

You may want to check out the "Sending Transactions" section of https://programtheblockchain.com/posts/2017/12/13/building-decentralized-apps-with-ethereum-and-javascript/.

  • Am calling themethods in my browser Console, and am giving it time but there is no response, – domii Dec 14 '17 at 17:02
  • Waiting sufficiently long (probably less than a second when using testrpc) between calling setInstructor and getInstructor should also work. I'm not sure what you mean when you say "there is no response." – smarx Dec 14 '17 at 17:03
  • What do you mean with "probably less than a second" I think it should be more, nevertheless I waited for more than 30 minutes and still no response. – domii Dec 15 '17 at 14:20
  • testrpc (now ganache) is generally set to mine instantly. – smarx Dec 15 '17 at 14:30
  • Again, I don't know what you mean by "there is no response." What do you expect to happen, and what's happening instead? – smarx Dec 15 '17 at 14:30

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.