Adding asset to participant in hyperledger-composer: Error Message “Model violation …”

Clash Royale CLAN TAG#URR8PPPAdding asset to participant in hyperledger-composer: Error Message “Model violation …”
In my hyperledger-composer application, I have Consultants and Skills. Furthermore, I have a transaction called "UpdateSkillsOfConsultant", with which a skill can be added to a consultant.
However, submitting the transaction results in the following error message:

I have no idea what I am supposed to do with this error message.
I created a minimal example, that can be copied and pasted into the composer -playground.
This is the stuff to be copied into the model.cto file:
namespace org.comp.myapp
abstract participant User identified by id {
o String id
o String firstName
o String lastName
o String email
o String password
}
participant Consultant extends User {
--> Skill skills optional
}
asset Skill identified by id {
o String id
o String name
o Proficiency proficiency
}
enum Proficiency {
o Beginner
o Intermediate
o Advanced
}
transaction UpdateSkillsOfConsultant {
--> Consultant consultant
--> Skill newSkills
}
event ConsultantUpdated {
o Consultant consultantOld
o Consultant consultantNew
}
And here is the content of the script.js file:
'use strict';
/**
* transaction UpdateSkillsOfConsultant
* @param {org.comp.myapp.UpdateSkillsOfConsultant} transaction
* @transaction
*/
async function updateSkillsOfConsultant(transaction) {
// Save the old version of the consultant:
const consultantOld = transaction.consultant;
// Update the consultant with the new skills:
const existingSkills = consultantOld.skills;
for (newSkill in transaction.newSkills) {
if (!transaction.consultant.skills) {
transaction.consultant.skills = [newSkill];
}
else {
transaction.consultant.skills = transaction.consultant.skills.concat(newSkill);
}
}
// Get the participant registry containing the consultants:
const participantRegistry = await getParticipantRegistry('org.comp.myapp.Consultant');
// Update the consultant in the participant registry:
await participantRegistry.update(transaction.consultant);
// Emit an event for the modified consultant:
let event = getFactory().newEvent('org.comp.myapp', 'ConsultantUpdated');
event.consultantOld = consultantOld;
event.consultantNew = transaction.consultant;
emit(event);
}
//helper function:
function findSkill(array, name) {
if(array) {
for (let i=0; i<array.length; i++) {
if (array[i].name == name) {
return array[i];
}
}
}
return null;
}
To reproduce the error just copy and paste everything in the composer playground, create a Consultant, create a Skill and then try to submit transaction "org.comp.myapp.UpdateSkillsOfConsultant".
1 Answer
1
This is a javascript issue. The line
for (newSkill in transaction.newSkills) {
for (newSkill in transaction.newSkills) {
is returning the keys of the array (which would be 0,1,2...) if you only pass in 1 value to the array then it returns the value 0 which is the error you are seeing. Change the line to
0
for (newSkill of transaction.newSkills) {
for (newSkill of transaction.newSkills) {
that will fix your problem.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.