Javascript cant update assets in for-loop

0 votes

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 too 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);
    }
}
Sep 17, 2018 in Blockchain by digger
• 26,740 points
631 views

1 answer to this question.

0 votes

Your for-loop will not wait for the asynchronous tasks to finish before continuing to the next iteration. What you could do is use recursion instead:

var amt = [200, 500, 700];
var netNumber = ['aa', 'bb', 'cc'];

async function GenerateNet(i = 0){
    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);

    const nextIndex = i++;
    if (nextIndex < amt.length) {
        return GenerateNet(nextIndex);
    }
}

await GenerateNet();
// All iterations have finished
answered Sep 17, 2018 by slayer
• 29,370 points

Related Questions In Blockchain

+1 vote
0 answers
+1 vote
1 answer

What is the best way to search for an item in blockchain?

All transactions and records in blockchain are ...READ MORE

answered Apr 21, 2018 in Blockchain by Perry
• 17,100 points
1,900 views
0 votes
1 answer
0 votes
1 answer

What could be the best term to use for the collection of contracts in a .sol file?

module - don't think so. Because module ...READ MORE

answered Jun 2, 2018 in Blockchain by Shashank
• 10,400 points
730 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,427 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,922 views
0 votes
1 answer

How to round a number down to 8 decimal places if its over 8 decimal places in Javascript

Try it in this way: function nrOfDecimals(number) { ...READ MORE

answered Sep 12, 2018 in Blockchain by slayer
• 29,370 points
1,511 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP