【发布时间】:2018-10-10 15:13:19
【问题描述】:
我目前正在 Hyperledger composer 中开发交易。该交易将创建 2 个不同的资产:资产 A 和资产 B。 资产 A 有一个类型为资产 B 的字段。所以我首先需要创建资产 B,然后创建具有资产 B 链接的资产 A。经过几次努力,我已经设法实现了逻辑。现在对等端返回以下错误:
2018-10-10T14:06:14.022Z [5ad2a944] 调试:NodeDataCollection :add() >assetAIdValue,{“$class”:“mynamespace.assetA”,“assetAId”:“assetAIdValue”,“assetAFieldX”: "assetAFieldXValue","assetB": ["resource:mynamespace.assetB#assetBIdValue1", "resource:mynamespace.assetB#assetBIdValue2"]},"$registryType":"Asset","$registryId":"mynamespace.assetA" },错误
(node:17) UnhandledPromiseRejectionWarning: UnhandledPromiseRejectionWarning: Unhandled Promise Rejection (rejection id: 2): Error: Error when created AssetA with idassetAIdValue
错误:[5ad2a944]GetState 没有分类帐上下文。发送错误
这是我的模型
namespace mynamespace
/**
* Definition of createAssetA transaction
*/
transaction createAssetA{
o String assetAId
o String assetAFieldX
o AssetB[] assetB
}
/**
* Definition of AssetA asset
*/
asset AssetA identified by assetAId{
o String assetAId
o String assetAFieldX
--> AssetB[] assetB
}
/**
* Definition of assetB asset
*/
asset AssetB identified by AssetBId{
o String assetBId
o String assetBFieldX
o String assetBFieldY
}
这是我的 logic.js
/**
* Creates the assets assetB and assetA
* @param {mynamespace.createAssetA} createAssetA - the AssetA to create
* @transaction
*/
function createAssetA(createAssetA) {
return getAssetRegistry('mynamespace.AssetB')
.then(function (assetBRegistry) {
// Add all the AssetBs to the AssetB registry
assetBRegistry.addAll(createAssetA.assetB)
.then(function(){
addAssetA(createAssetA);
}, function(error) {
throw new Error ('Error while creating AssetBs' + '\n' + error);
} );
});
}
/**
* Function to add assetA asset to registry
* @param {*} createAssetA - The createAssetA Transaction containing the assetA info
*/
function addAssetA(createAssetA){
return getAssetRegistry('mynamespace.AssetA')
.then(function (assetARegistry) {
var newAssetA = castcreateAssetATxToAsset(createAssetA);
return assetARegistry.add(newAssetA).catch(function (error) {
throw new Error ('Error when creating AssetA with id ' + createAssetA.id + '\n' + error);
});
});
}
/**
* Casts the createAssetA transaction as a AssetA Asset
* @param {*} createAssetA - The transaction createAssetA to cast
* @returns {mynamespace.AssetA} the AssetA Asset with the info from the createAssetA param
*/
function castcreateAssetATxToAsset(createAssetA){
var factory = getFactory();
// Create a new instance of AssetA class from the mynamespace namespace with the id createAssetA.id
var newAssetA = factory.newResource('mynamespace', 'AssetA', createAssetA.id);
newAssetA.assetAFieldX = createAssetA.assetAFieldX;
newAssetA.assetB = [];
for (var i = 0; i < createAssetA.assetB.length; i++) {
var assetB = factory.newRelationship('mynamespace', 'assetB', createAssetA.assetB[i].id);
newAssetA.assetB[i] = assetB;
}
return newAssetA;
}
这是我请求的内容
{
"$class": "mynamespace.createAssetA",
"assetAId": "assetAIdValue",
"assetAFieldX": "assetAFieldXValue",
"assetB": [
{
"$class": "mynamespace.assetB",
"assetBId": "assetBIdValue1",
"assetBFieldX": "assetBFieldXValue",
"assetBFieldY": "assetBFieldYValue"
},
{
"$class": "mynamespace.assetB",
"assetBId": "assetBIdValue2",
"assetBFieldX": "assetBFieldXValue",
"assetBFieldY": "assetBFieldYValue"
}
]
}
如果有人知道这种错误的原因,他会拯救我的一天!
另一个问题: 这个错误只有在我查看链码容器时才可见。任何人都知道如何让它们在 Composer REST 服务器上可见?
【问题讨论】:
标签: hyperledger hyperledger-composer