1

I have written a solidity contract deployed on Ropsten network. I am using lite-server, NodeJS to develop a front-end. I have figured out how to use events and I can access a getter with one return value:

 myBorrower.getBorrowerName(function(err, res) {
    $("#loader").show();
    if (!err) {
        $("#borrower").html('Borrower Name: ' + web3.toAscii(res));
        $("#loader").hide();
    }
});

but I cannot figure out how to access a getter with 2 values in the callback function in web3. The Javascript code is:

   myPortfolio.getPortfolioInfo(function(err, res) {
        $("#loader").show();
        if (!err) {
            $("#portfolio").html('Portfolio Name: ' + web3.toAscii(value.args[0]) + ' Ether Balance: ' + web3.toAscii(value.args[1]));
            $("#loader").hide();
        }
    });

I know that args[0] is not correct, but I cannot figure out what it should be.

The relevant solidity function is:

    function getPortfolioInfo() public view returns (bytes16, uint){

    return (portfolioName, portfolioBalance);
}

Can anyone help me with this please? Thanks.

6
  • 3
    I think it's just res[0] and res[1], but it should be easy to see by just doing console.log(res), right?
    – user19510
    Commented Mar 4, 2018 at 0:26
  • You are right! Thanks. I had tried that before but it did not work. The reason though was that I applied web3.toAscii to both res[0] and res[1] but res[1] is a uint not bytes16 so cannot be converted to Ascii. When I use the following it works - $("#portfolio").html('Portfolio Name: ' + web3.toAscii(res[0]) + ' Balance: ' + res[1]); Commented Mar 4, 2018 at 10:25
  • I have one follow up question. if I return an array from getPortfolioInfo and I want to access it, how do I do so? Would it be res[0][0] where [0] is the first element in the array? Any idea how that could work? Commented Mar 8, 2018 at 21:41
  • This is a really simple thing to just try yourself.
    – user19510
    Commented Mar 8, 2018 at 21:48
  • I am sure it is really simple and I feel pretty silly. I cannot figure it out. Could you please help me though? I would really appreciate it. What I am trying is $("#loans").html('Loan ID: ' + web3.toAscii(res[4][0].loanID)); The fourth value passed is an array of addresses with a bytes16 loanID. Commented Mar 8, 2018 at 22:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.