将关系添加到 hyperledger-composer 中的关系数组
Adding relationships to an array of relationships in hyperledger-composer
在我的 hyperledger composer 应用程序中,我有客户和顾问。客户具有对已添加到 his/her "readAccessList".
的顾问的读取权限
以下是这两种参与者类型的模型:
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
}
添加顾问到客户readAccessList的事务定义如下:
transaction AddToReadableConsultantsOfClient {
--> Client client
--> Consultant[] newReadableConsultants
}
此交易的交易处理器函数如下:
/**
* 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);
}
在我的 angular 应用程序中,我尝试使用以下代码将几个顾问添加到客户的 readAccessList 中:
//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;
}
});
出于某种原因,这不起作用。顾问不会添加到客户的 "readAccessList"(保持为空)。
在控制台中我收到一条奇怪的错误消息,内容如下:
Error: 2 UNKNOWN: error executing chaincode: transaction returned with failure: ReferenceError: org is not defined
我将关系添加到关系数组的方法有什么问题?
我更改了您交易脚本的 1 行:
transaction.client.readAccessList.concat(transaction.newReadableConsultants);
至
transaction.client.readAccessList=transaction.client.readAccessList.concat(transaction.newReadableConsultants);
我在 Composer Playground 中进行了测试,我使用的 JSON 数据是:
{
"$class": "org.comp.myapp.AddToReadableConsultantsOfClient",
"client": "resource:org.comp.myapp.Client#MBC01",
"newReadableConsultants": [
"resource:org.comp.myapp.Consultant#C02"
]
}
交易现在可以在 Playground 中运行 - 但我还没有通过 Angular 应用程序进行测试。
在我的 hyperledger composer 应用程序中,我有客户和顾问。客户具有对已添加到 his/her "readAccessList".
的顾问的读取权限以下是这两种参与者类型的模型:
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
}
添加顾问到客户readAccessList的事务定义如下:
transaction AddToReadableConsultantsOfClient {
--> Client client
--> Consultant[] newReadableConsultants
}
此交易的交易处理器函数如下:
/**
* 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);
}
在我的 angular 应用程序中,我尝试使用以下代码将几个顾问添加到客户的 readAccessList 中:
//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;
}
});
出于某种原因,这不起作用。顾问不会添加到客户的 "readAccessList"(保持为空)。
在控制台中我收到一条奇怪的错误消息,内容如下:
Error: 2 UNKNOWN: error executing chaincode: transaction returned with failure: ReferenceError: org is not defined
我将关系添加到关系数组的方法有什么问题?
我更改了您交易脚本的 1 行:
transaction.client.readAccessList.concat(transaction.newReadableConsultants);
至
transaction.client.readAccessList=transaction.client.readAccessList.concat(transaction.newReadableConsultants);
我在 Composer Playground 中进行了测试,我使用的 JSON 数据是:
{
"$class": "org.comp.myapp.AddToReadableConsultantsOfClient",
"client": "resource:org.comp.myapp.Client#MBC01",
"newReadableConsultants": [
"resource:org.comp.myapp.Consultant#C02"
]
}
交易现在可以在 Playground 中运行 - 但我还没有通过 Angular 应用程序进行测试。