Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Blockchain by (12.8k points)

I am trying to use for-loop in the transaction. The for-loop basically creates new assets, ie. I am creating a new asset on each iteration.

The problem is that each time the loop iterates the value of vals.length remains zero(0). And if I try to do console.log(vals) then to it shows null array even when loop iterates for i=1,2,3...

But once whole transaction function gets completed then I am able to see the assets in UI.


asset Net identified by NetNumber{

    o String NetNumber

    o Integer totaltransaction


transaction GenerateNet {




* submit an order

* @param {org.demo.invoice.GenerateNet}

* @transaction


async function GenerateNet(){

    var amt = [200, 500, 700];

    var netNumber = ['aa', 'bb', 'cc'];

    for(var i=0; i < amt.length; i++){

        let netRegistry = await getAssetRegistry('org.demo.invoice.Net');

        let vals = await netRegistry.getAll();



        let newNetting = factory.newResource('org.demo.invoice','Net', netNumber);

        newNetting.totaltransaction = amt[i];

        await nettingRegistry.add(newNetting);



I want to see the value of vals.length to be 1,2,3 as the iteration goes on.

1 Answer

0 votes
by (29.8k points)

New assets are not added to the ledger until the transaction is endorsed. So, at the time the code is run, none of the assets are actually added, they are scheduled to be added if/when the transaction is approved.In other words, you cannot add an asset and read it inside the same transaction.

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers


94k users

Browse Categories