Reader small image

You're reading from  Learn Blockchain Programming with JavaScript

Product typeBook
Published inNov 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781789618822
Edition1st Edition
Languages
Tools
Concepts
Right arrow
Author (1)
Eric Traub
Eric Traub
author image
Eric Traub

Eric Traub currently works as a software engineer in New York City. He has extensive experience working as a teacher and instructing people in a variety of different subjects. He changed his career from teaching to software engineering because of the excitement it brings to him and the passion that he has for it. He is now lucky enough to have the opportunity to combine both of these passions - software engineering and teaching!
Read more about Eric Traub

Right arrow

Defining the getTransaction method

Let's add a new method on the blockchain data structure called getTransaction. This will allow us to get a specific transaction by passing transactionId. We'll use this new method inside of the /transaction/:transactionId endpoint. So, let's get started!

  1. Go to the dev/blockchain.js file, and after the getBlock method, define the getTransaction as follows:
Blockchain.prototype.getTransaction = function(transactionId) { 

}):

This method is very similar to the getBlock method. Here, we'll iterate through the entire chain and will set a flag equal to the correct transaction that we are looking for.

  1. The next step in building this method will be to iterate through the entire blockchain. For this, use the forEach loop as follows:
Blockchain.prototype.getTransaction = function(transactionId) { 
this.chain.forEach(block...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Learn Blockchain Programming with JavaScript
Published in: Nov 2018Publisher: PacktISBN-13: 9781789618822

Author (1)

author image
Eric Traub

Eric Traub currently works as a software engineer in New York City. He has extensive experience working as a teacher and instructing people in a variety of different subjects. He changed his career from teaching to software engineering because of the excitement it brings to him and the passion that he has for it. He is now lucky enough to have the opportunity to combine both of these passions - software engineering and teaching!
Read more about Eric Traub