Tyler
Tyler

Reputation: 51

Updating Existing Assets - Hyperledger Composer

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

My model file looks like this:

asset Loan identified by loanNum{
    o String loanNum
    o Double balance
}

transaction createTransaction {
    o String loanNum
    o Double transAmount
}

My script looks like this:

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);
                      })                             
          }
      })       

}

Script Summary:

  1. Two values are fed to the script, a loan number and a transaction amount.
  2. The script then returns the asset registry and returns the appropriate bool if the loan number exists already.
  3. If False(loan doesnt exists in asset registry), a new asset will be created using the loan number and balance from the transaction.
  4. If True, the asset registry is returned then I use the .get function and set the results equal to the loanToUpdate variable that is created at the start of the script. I then use the .update(loanToUpdate) function on the asset registry to update the existing asset.

When bool is False the script works correctly and creates a new asset, but when bool is True and the transaction contains loan number that already exists I get the error "Error: Expected a Resource or Concept."

EDIT

Now that i am getting more experience with this, i think it can be done much easier with built in relationships but ill post my updated functional code below.

Updated Code with Fix:

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){                               
                    return assetRegistry2.get(tx.loanNum); 
                })
                .then(function(updateloan){
                        loanToUpdate = updateloan                      
                        loanToUpdate.balance = tx.transAmount;
                        return getAssetRegistry(NS + '.Loan')

                })
                .then(function(assetRegistry3){
                        return assetRegistry3.update(loanToUpdate);
                })
        } 
        else {
                return getAssetRegistry(NS + '.Loan')
                    .then(function(assetRegistry2){

                        var newLoan = factory.newResource(NS,'Loan',tx.loanNum);                            
                        newLoan.balance = tx.transAmount;                           
                        return assetRegistry2.add(newLoan);
                    })                             
        }
    })       

}

Upvotes: 2

Views: 2239

Answers (1)

david_k
david_k

Reputation: 5868

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.

Upvotes: 3

Related Questions