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
• 27,630 points
299 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
• 67,290 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,010 points
190 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
• 27,630 points
97 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
• 27,630 points
332 views
0 votes
1 answer
+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
298 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
• 67,290 points
33 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
• 67,290 points
350 views