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