I have setup a local network running with bootnode,node1,node2,signer node When I sent transaction from geth js console, I am able to see the receipt immediately:
eth.getTransactionReceipt("0x1858f9b411d75b76ac5c91bb2d5edd79ba7f289ef8ac6dcce20f9f1aa9302024")
{ blockHash: "0x41793542b33766175ade09addc238325e7167512fd2b7316cbca4f7d4ee5dee3", blockNumber: 982, contractAddress: null, cumulativeGasUsed: 21000, effectiveGasPrice: 1000000007, from: "0xc4ffca9978011b3bd69975b51706a055424c6feb", gasUsed: 21000,
logs: [], logsBloom:"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", status: "0x1", to: "0xa4c9f4d914aa525487544bfc09334aa2146e6f3c",
transactionHash: "0x1858f9b411d75b76ac5c91bb2d5edd79ba7f289ef8ac6dcce20f9f1aa9302024", transactionIndex: 0, type: "0x2" }
However when I run it from VS code terminal
const WS = require("ws"); const { Web3 } = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider('http://127.0.0.1:8545')); rcpt = web3.eth.getTransactionReceipt("0x1858f9b411d75b76ac5c91bb2d5edd79ba7f289ef8ac6dcce20f9f1aa9302024"); console.log(rcpt); web3.eth.getTransactionReceipt("0x1858f9b411d75b76ac5c91bb2d5edd79ba7f289ef8ac6dcce20f9f1aa9302024", function(error,result){ console.log(result); });
It returns following in the first call, the second call doest not print anything.
Promise { }
Why is it failing when I run from VS code console?
The function getTransactionReceipt
(just as other web3 functions) returns a Promise
. In other words, the flow is asynchronous but your code is synchronous. You need to either wrap it in an async
function and use the await
keyword, or, use the Promise.then()
approach, i.e.
const WS = require("ws"); const { Web3 } = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider('http://127.0.0.1:8545'));
web3.eth.getTransactionReceipt("0x1858f9b411d75b76ac5c91bb2d5edd79ba7f289ef8ac6dcce20f9f1aa9302024")
.then(console.log);
For more info see the documentation at https://web3js.readthedocs.io/en/v1.2.11/web3-eth.html#gettransactionreceipt