Search code examples
hyperledger-composerrelationships

Adding relationships to an array of relationships in hyperledger-composer


In my hyperledger composer app, I have Clients and Consultants. A Client has read access to Consultants that have been added to his/her "readAccessList".

Here are the models of these two participant types:

participant Client identified by id {
  o String id
  o String name
  --> Consultant[] readAccessList optional
}

participant Consultant identified by id {
  o String id
  o String name
  o String text
}

The transaction for adding consultants to the readAccessList of clients is defined as follows:

transaction AddToReadableConsultantsOfClient {
  --> Client client
  --> Consultant[] newReadableConsultants
}

The transaction processor function for this transaction is the following:

/**
 * transaction AddToReadableConsultantsOfClient
 * @param {org.comp.myapp.AddToReadableConsultantsOfClient} transaction
 * @transaction
 */
async function addToReadableConsultantsOfClient(transaction) {

    // Save the old version of the client:
    const clientOld = transaction.client;

    // Update the client with the new readableConsultants:
    transaction.client.readAccessList.concat(transaction.newReadableConsultants);    

    // Get the participant registry containing the clients:
    const participantRegistry = await getParticipantRegistry('org.comp.myapp.Client');

    // Update the client in the participant registry:
    await participantRegistry.update(transaction.client);

    // Emit an event for the modified client:
    let event = getFactory().newEvent('org.comp.myapp', 'ClientUpdated');
    event.clientOld = clientOld;
    event.clientNew = transaction.client;
    emit(event);

}

In my angular app, I try to add a couple of consultants to the readAccessList of a Client, using the following code:

//Note that the consultants referred to in the following array do actually exist:
let consultants = ["resource:org.comp.myapp.Consultant#1", "resource:org.comp.myapp.Consultant#2"];

this.transaction = {
  $class: "org.comp.myapp.AddToReadableConsultantsOfClient",       
      "client": "resource:org.comp.myapp.Client#" + this.clientId,
      "newReadableConsultants": consultants,
      "timestamp": new Date().getTime()
};

return this.addToReadableConsultantsOfClientService.addTransaction(this.transaction)
.toPromise()
.then(() => {
  this.errorMessage = null;
})
.catch((error) => {
    if(error == 'Server error'){
      this.errorMessage = "Could not connect to REST server. Please check your configuration details.";
    }
    else if(error == '404 - Not Found'){
      this.errorMessage = "404 - Could not find API route. Please check your available APIs."
    }
    else{
      this.errorMessage = error;
    }
});

For some reason, this doesn't work. The consultants do not get added to the "readAccessList" of the Client (which stays empty).

In the console I'm getting a strange error message, saying the following:

Error: 2 UNKNOWN: error executing chaincode: transaction returned with failure: ReferenceError: org is not defined

What is wrong with my approach to adding relationships to an array of relationships?


Solution

  • I changed 1 line of your transaction script:

    transaction.client.readAccessList.concat(transaction.newReadableConsultants);

    to

    transaction.client.readAccessList=transaction.client.readAccessList.concat(transaction.newReadableConsultants);

    I tested in the Composer Playground and the JSON data I used was:

    {
     "$class": "org.comp.myapp.AddToReadableConsultantsOfClient",
     "client": "resource:org.comp.myapp.Client#MBC01",
     "newReadableConsultants": [
      "resource:org.comp.myapp.Consultant#C02"
     ]
    }
    

    The transaction now works in Playground - but I haven't tested through the Angular App.