Hyperledger Composer How to Update Existing Assets

0 votes

I am trying to create a simple hyperledger chaincode in composer that updates the asset associated  if it exists, else creates a new asset.

My code:

function createTransaction(tx) {
    var NS = 'org.acme.ra';
    var factory = getFactory();
    var loanToUpdate

//returns all assets 
  return getAssetRegistry(NS + '.Loan')
      .then(function(assetRegistry){
       return assetRegistry.exists(tx.loanNum);  
      })
      .then(function(exists){
          if (exists) {
                  return getAssetRegistry(NS + '.Loan')                         
                      .then(function(assetRegistry2){                            
                          loanToUpdate =  assetRegistry2.get(tx.loanNum)
                          loanToUpdate.balance = tx.transAmount;
                          return assetRegistry2
                      })
                      .then(function(updateAssetRegistry){
                          return updateAssetRegistry.update(loanToUpdate)//broken right here
                  })
          } 
          else {
                  return getAssetRegistry(NS + '.Loan')
                      .then(function(assetRegistry2){
                       var newLoan =factory.newResource(NS,'Loan',tx.loanNum);
                          newLoan.balance = tx.transAmount;                         
                          return assetRegistry2.add(newLoan);
                      })                             
          }
      })       
Nov 5, 2018 in Blockchain by digger
• 26,740 points
1,429 views

1 answer to this question.

0 votes

get on an asset registry returns a promise, so the line  loanToUpdate = assetRegistry2.get(tx.loanNum) needs to be part of the promise chain and so you need a .then block to process the returned value before you can update it.

answered Nov 5, 2018 by Omkar
• 69,230 points

Related Questions In Blockchain

0 votes
1 answer

How to get the already existing channels in Hyperledger v1.0?

You cannot see all available channels,  but you ...READ MORE

answered Jun 4, 2018 in Blockchain by Perry
• 17,100 points
2,460 views
0 votes
1 answer

How to control visibility of assets in Hyperledger Fabric?

you can use proof of Authority consensus ...READ MORE

answered Jul 10, 2018 in Blockchain by digger
• 26,740 points
763 views
0 votes
1 answer

How to solve EACCES:permission denied error in Hyperledger Composer?

It seems like there is no required ...READ MORE

answered Jul 11, 2018 in Blockchain by digger
• 26,740 points
2,359 views
0 votes
1 answer

How to solve "error trying install composer runtime" in Hyperledger composer?

I think the docker-compose tool is not ...READ MORE

answered Jul 17, 2018 in Blockchain by slayer
• 29,370 points
859 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,401 views
+1 vote
1 answer
0 votes
1 answer

Hyperledger remove asset

Data on a blockchain won't ever be ...READ MORE

answered Jul 4, 2018 in Blockchain by Christine
• 15,790 points
1,283 views
0 votes
1 answer

Hyperledger Composer: How to update a contract after it is saved?

Try this and see if this works: /** * ...READ MORE

answered Nov 9, 2018 in Blockchain by Omkar
• 69,230 points
758 views
0 votes
1 answer

Hyperledger Composer: How to retrieve array of assets?

This is what worked for me: asset Ask ...READ MORE

answered Nov 14, 2018 in Blockchain by Omkar
• 69,230 points
1,659 views
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