Ethereum nodejs Contract address returning as undefined in console

0 votes
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

var accounts_list = web3.eth.accounts;
var code = fs.readFileSync('Voting.sol').toString();
var compiledCode = solc.compile(code);
var abiDefinition = JSON.parse(compiledCode.contracts[':Voting'].interface)
var VotingContract = web3.eth.contract(abiDefinition)
var byteCode = compiledCode.contracts[':Voting'].bytecode
var deployedContract = VotingContract.new(['Itachi','Luffy','Midoriya'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000})
var deployedContractAddress = deployedContract.address;

var contractInstance = VotingContract.at(deployedContract.address);
// contractInstance.voteForCandidate('Itachi', {from: web3.eth.accounts[0]}) // function to vote for Itachi
// contractInstance.totalVotesFor.call('Itachi').toLocaleString() // function to return Itachi's votes

var deployedAddress = contractInstance.address;

console.log(contractInstance.address);

the ouput is: undefined

but when i manually run each command on node Console it is not the case. when i tried typeof contractInstance.address it outputs as "String"

but i dont want to run each command manually every time hence tried running it in a script

Oct 15, 2018 in Blockchain by slayer
• 29,370 points
1,871 views

1 answer to this question.

0 votes

As per the documentation, if you deploy the contract synchronously, the function returns immediately with a transaction hash, but you'll need to poll the transaction's status until it's mined. (Only then will the contract's address be available.)

As an alternative, you can deploy the contract asynchronously:

VotingContract.new(['Itachi','Luffy','Midoriya'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000}, function (err, deployedContract) {
    if (deployedContract.address) {
        console.log(`Address: ${deployedContract.address}`);
        // use deployedContract here
    }
});
answered Oct 15, 2018 by Omkar
• 69,220 points

Related Questions In Blockchain

0 votes
1 answer

Where are the transaction details of a smart contract is written in Ethereum?

First ques:where in the blockchain is a ...READ MORE

answered Jul 11, 2018 in Blockchain by Shashank
• 10,400 points
812 views
0 votes
1 answer

Error in sending value to an Ethereum Smart Contract.

It is stated in Solidity 4.0 documentation ...READ MORE

answered Jul 23, 2018 in Blockchain by Perry
• 17,100 points
1,414 views
+1 vote
1 answer
0 votes
1 answer

Smart contract limitations in Ethereum and Hyperledger Fabric

No, the contract cannot sign on behave of B. A ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
673 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,923 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,428 views
0 votes
1 answer
0 votes
1 answer

Nodejs Express: Getting different output in console.log and callback

If your getOrderStatus() function is like this: function getOrderStatus(_orderId, callback) ...READ MORE

answered Oct 9, 2018 in Blockchain by Omkar
• 69,220 points
505 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP