Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Blockchain by (12.7k 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.

model.cto

asset Net identified by NetNumber{

    o String NetNumber

    o Integer totaltransaction

}

transaction GenerateNet {

}

logic.js

/**

* 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();

        console.log(vals.length); 

        console.log(vals);

        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.5k points)
edited by

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.

You can learn more about Blockchain by enrolling in Blockchain Certification Course by Intellipaat

Browse Categories

...