向 hyperledger-composer 中的参与者添加资产:错误消息 "Model violation ..."
Adding asset to participant in hyperledger-composer: Error Message "Model violation ..."
在我的 hyperledger-composer 应用程序中,我有顾问和技能。此外,我有一个名为 "UpdateSkillsOfConsultant" 的事务,可以将技能添加到顾问中。
但是,提交交易会导致出现以下错误消息:
我不知道我应该如何处理这个错误消息。
我创建了一个最小示例,可以将其复制并粘贴到 composer -playground 中。
这是要复制到 model.cto 文件中的内容:
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
}
这里是 script.js 文件的内容:
'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;
}
要重现错误,只需复制并粘贴 composer playground 中的所有内容,创建顾问,创建技能,然后尝试提交交易 "org.comp.myapp.UpdateSkillsOfConsultant"。
这是一个 javascript 问题。行
for (newSkill in transaction.newSkills) {
返回数组的键(即 0,1,2...),如果您只将 1 个值传递给数组,那么它 returns 的值 0
是您看到的错误。将行更改为
for (newSkill of transaction.newSkills) {
这将解决您的问题。
在我的 hyperledger-composer 应用程序中,我有顾问和技能。此外,我有一个名为 "UpdateSkillsOfConsultant" 的事务,可以将技能添加到顾问中。
但是,提交交易会导致出现以下错误消息:
我不知道我应该如何处理这个错误消息。
我创建了一个最小示例,可以将其复制并粘贴到 composer -playground 中。
这是要复制到 model.cto 文件中的内容:
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
}
这里是 script.js 文件的内容:
'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;
}
要重现错误,只需复制并粘贴 composer playground 中的所有内容,创建顾问,创建技能,然后尝试提交交易 "org.comp.myapp.UpdateSkillsOfConsultant"。
这是一个 javascript 问题。行
for (newSkill in transaction.newSkills) {
返回数组的键(即 0,1,2...),如果您只将 1 个值传递给数组,那么它 returns 的值 0
是您看到的错误。将行更改为
for (newSkill of transaction.newSkills) {
这将解决您的问题。