Hyperledger Composer:如何将一系列资产推入另一个资产领域?
Hyperledger Composer: How to push array of assets into another asset's field?
我正在尝试在 Composer 中重新创建一个用 GoLang 编写的链代码。
Model.cto
asset Carton identified by cartonId {
o String cartonId
o String manufacturerId
o String dateOfDeparture optional
o String recipient optional
o String currentOwner
o String status
--> Unit[] units optional
o Trackrecord[] trackrecord optional
}
transaction MakeCarton {
--> Unit[] unit
o String Id
}
asset Unit identified by unitId {
o String unitId
o String cartonId
o String manufacturerId
o String dateOfDeparture
o String recipient
o Trackrecord[] trackrecord
o String currentOwner
o String status
}
所以我需要创建交易来创建一个纸箱并接受单位数组和 cartonId。
function makeCarton(make) {
var carton = getFactory().newResource('org.acme.mynetwork','Carton',make.Id);
//carton.cartonId = make.Id ;
var unitobjs = new Array() ;
for(var i=0; i < make.unit.length ; i++) {
var unitobj = getFactory().newResource('org.acme.mynetwork','Unit',make.unit[i].unitId) ;
unitobj = getFactory().newRelationship('org.acme.mynetwork','Unit',make.unit[i].unitId) ;
unitobjs.push(unitobj) ;
}
for(var i = 0 ; i< make.unit.length ; i++) {
carton.units.push(make.unit[i]) ;
}
// getFactory().newRelationship('org.acme.mynetwork','Unit',make.unit[i].unitId)
carton.manufacturerId= make.unit.manufacturerId ;
carton.currentOwner = make.unit.currentOwner ;
carton.status = 'At '+ make.unit.currentOwner ;
return getAssetRegistry('org.acme.mynetwork.Carton').then(function (assetRegistry) {
return assetRegistry.add(carton);
});
}
提交此交易会产生一个错误:carton.units未定义
您没有初始化 carton.units
。您可以只复制数组,使用:
carton.units = make.unit;
就是说,我不太了解您的模型 -- carton
有一个单元数组,每个单元都有一个所有者,但是纸箱也有一个所有者。
我遇到了同样的问题。而这个post帮我解决了。然而,对我来说,
transaction MakeCarton {
--> Unit[] unit
o String Id
}
必须更改为
transaction MakeCarton {
o Unit[] unit
o String Id
}
否则会出现以下错误:
请求 POST /api/MakeCarton 的未处理错误:错误:JSON 数据无效。找到一个不是字符串的值:[object Object],[object Object] for relationship RelationshipDeclaration {name=unit, type=org.acme.mynetwork.Unit, array=true, optional=true}
在 JSONPopulator.visitRelationshipDeclaration (/usr/local/lib/node_modules/composer-rest-server/node_modules/composer-common/lib/serializer/jsonpopulator.js:264:31)
我正在尝试在 Composer 中重新创建一个用 GoLang 编写的链代码。 Model.cto
asset Carton identified by cartonId {
o String cartonId
o String manufacturerId
o String dateOfDeparture optional
o String recipient optional
o String currentOwner
o String status
--> Unit[] units optional
o Trackrecord[] trackrecord optional
}
transaction MakeCarton {
--> Unit[] unit
o String Id
}
asset Unit identified by unitId {
o String unitId
o String cartonId
o String manufacturerId
o String dateOfDeparture
o String recipient
o Trackrecord[] trackrecord
o String currentOwner
o String status
}
所以我需要创建交易来创建一个纸箱并接受单位数组和 cartonId。
function makeCarton(make) {
var carton = getFactory().newResource('org.acme.mynetwork','Carton',make.Id);
//carton.cartonId = make.Id ;
var unitobjs = new Array() ;
for(var i=0; i < make.unit.length ; i++) {
var unitobj = getFactory().newResource('org.acme.mynetwork','Unit',make.unit[i].unitId) ;
unitobj = getFactory().newRelationship('org.acme.mynetwork','Unit',make.unit[i].unitId) ;
unitobjs.push(unitobj) ;
}
for(var i = 0 ; i< make.unit.length ; i++) {
carton.units.push(make.unit[i]) ;
}
// getFactory().newRelationship('org.acme.mynetwork','Unit',make.unit[i].unitId)
carton.manufacturerId= make.unit.manufacturerId ;
carton.currentOwner = make.unit.currentOwner ;
carton.status = 'At '+ make.unit.currentOwner ;
return getAssetRegistry('org.acme.mynetwork.Carton').then(function (assetRegistry) {
return assetRegistry.add(carton);
});
}
提交此交易会产生一个错误:carton.units未定义
您没有初始化 carton.units
。您可以只复制数组,使用:
carton.units = make.unit;
就是说,我不太了解您的模型 -- carton
有一个单元数组,每个单元都有一个所有者,但是纸箱也有一个所有者。
我遇到了同样的问题。而这个post帮我解决了。然而,对我来说,
transaction MakeCarton {
--> Unit[] unit
o String Id
}
必须更改为
transaction MakeCarton {
o Unit[] unit
o String Id
}
否则会出现以下错误:
请求 POST /api/MakeCarton 的未处理错误:错误:JSON 数据无效。找到一个不是字符串的值:[object Object],[object Object] for relationship RelationshipDeclaration {name=unit, type=org.acme.mynetwork.Unit, array=true, optional=true} 在 JSONPopulator.visitRelationshipDeclaration (/usr/local/lib/node_modules/composer-rest-server/node_modules/composer-common/lib/serializer/jsonpopulator.js:264:31)