2

I am using truffle as my Dapp development platform together with testrpc. When I attempt to create a new instance of the my contract using the .new() method, I am having an issue with printing out the contract details of this new contract instance to the console.log in the JS console of the browser. Specifically, I am triggering the invocation of the .new() method by using an jquery click() method which takes in the values of the contract constructor parameters in the browser. The code I am using is contained in an app.js file that is referenced in the index.html file and is set out below:

$("#clickButton").click(function() {

    var accounts = web3.eth.accounts;
    var Param1 = parseInt($("input#param1").val());
    var Param2 = parseInt($("input#param2").val());
    var Param3 = parseInt($("input#param3").val());

    console.log(Param1,Param2,Param3)

    var myContractInstance=myContract.new(Param1,Param2,Param3,{ from: accounts[0]});

    console.log(myContractInstance);

    myContractInstance
    .then(function(contract) {
    console.log('Contract created', contract)
    })
    .catch(function(err) {
    console.log('Error', err)
    });

The print out that I get in the JS console is as follows (note: that I have input values of 1111,222 and 3333 for my values for Param1, Param2 and Param3:

1111 222 3333 Promise {_bitField: 0, _fulfillmentHandler0: undefined, _rejectionHandler0: undefined, _promise0: undefined, _receiver0: undefined…} Error Error: Invalid JSON RPC response: ""(…)

It is clear that the values are being consumed properly (as they are being printed out). Also, it is clear that promise object is being created (as this has also correctly printed out). It seems to be erroring thereafter.

However, if I copy and paste the exact same code above into the browser JS console then I do not get the error above i.e. the contract is printed out in the console.log as expected with no issue. I am baffled as to why I get an error if I use the jquery click() method but I do not get an error if I punch the code directly into the JS console. Any help is much appreciated.

2

The default behavior of a button click is to submit a form. When you click the link, the page refreshes from the form submit, interrupting the web3 request.

Add return false to the end of your click handler to prevent the default browser action and it will work.

  • @thegooders if this solution solves your problem, please accept it – Joris Bontje Jul 8 '16 at 6:40
  • Actually, I think I have figured it out. If not, please let me know. – thegooders Jul 9 '16 at 16:41

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.