Web3.js: getTransaction breaks trying to access blockNumber of null response

Created on 13 Feb 2019  路  5Comments  路  Source: ChainSafe/web3.js

Expected behavior

The method getTransaction should return null or error for specifying a non-existent transaction.

Actual behavior

The method getTransaction breaks trying to access blockNumber of null response.

Steps to reproduce the behavior

  1. Connect to the main network (I used the _Infura_ provider to do so);
  2. Call getTransaction with a transaction hash that does not exist, like _0x6b39f5caf5907ae686ac42f91527ab6897047027ce688f35b9e28ec4a21e8667_, which is an existent transaction hash from the Rinkeby network.
this.web3.eth.getTransaction(transactionHash).catch(console.log);

Error Logs

TypeError: Cannot read property 'blockNumber' of null
      at Object.outputTransactionFormatter (node_modules/web3-core-helpers/dist/web3-core-helpers.cjs.js:91:14)
      at GetTransactionMethod.afterExecution (node_modules/web3-core-method/dist/web3-core-method.cjs.js:961:30)
      at GetTransactionMethod._callee$ (node_modules/web3-core-method/dist/web3-core-method.cjs.js:457:39)
      at tryCatch (node_modules/regenerator-runtime/runtime.js:62:40)
      at GeneratorFunctionPrototype.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:288:22)
      at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (node_modules/regenerator-runtime/runtime.js:114:21)
      at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
      at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
      at process._tickCallback (internal/process/next_tick.js:109:7)

Versions

NPM: v6.15.1
Node: v10.15.1
Web3.js: 1.0.0-beta.46 (latest)
OS: Ubuntu 16.04

bug

All 5 comments

This issue seems to be related but happens for sendTransaction. This one is for getTransaction alone.

Yup, you're right. The afterExecution processing is also missing the undefined return case. Guilty lines below.

https://github.com/ethereum/web3.js/blob/35ebbcacbecc75a6cac033fc648d0091e0235a83/packages/web3-core-method/src/methods/transaction/GetTransactionMethod.js#L45-L47

Thanks for submitting this issue! I will fix and release it asap.

This still happens on "1.0.0-beta.52"...
Reproduce:

const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.WebsocketProvider('wss://mainnet.infura.io/ws'));

const batch = new web3.BatchRequest();
batch.add(web3.eth.getTransaction.request('0x223b3f5892a7c20c333b68c4e95729100e7143a8c4ca8134502c3f0a2a87aadd', function (err, res) {
    console.log(err);
}));
batch.execute();
// TypeError: Cannot read property 'blockNumber' of null
// ...

Can be wrong, but looks like still happens on '1.2.1'.
Or tell me please where is my mistake...
Reproduce (via subscribe for pendingTransactions):
javascript web3.eth.subscribe('pendingTransactions') .on("data", async (txHash) => { try { let transaction = await web3http.eth.getTransaction(txHash); console.log(transaction.from); } catch (err) { console.log(err.message); } })

Was this page helpful?
0 / 5 - 0 ratings