Hyperledger Composer returns 错误 GetState 没有分类帐上下文
Hyperledger Composer returns error No ledger context for GetState
我目前正在 Hyperledger Composer 中开发交易。此交易将创建 2 个不同的资产:资产 A 和资产 B。
资产 A 有一个类型为资产 B 的字段。所以我首先需要创建资产 B,然后用 link 到资产 B 创建资产 A。经过一些努力,我成功地实现了逻辑。现在同行returns出现以下错误:
2018-10-10T14:06:14.022Z [5ad2a944] DEBUG :NodeDataCollection :add() > assetAIdValue, {"$class": "mynamespace.assetA","assetAId": "assetAIdValue","assetAFieldX": "assetAFieldXValue","assetB": ["resource:mynamespace.assetB#assetBIdValue1", "resource:mynamespace.assetB#assetBIdValue2"]},"$registryType":"Asset","$registryId":"mynamespace.assetA"}, false
(node:17) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Error when creating AssetA with id assetAIdValue
Error: [5ad2a944]No ledger context for GetState. Sending ERROR
这是我的模型
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 服务器可见?
看起来您在承诺链中的代码中缺少视图 return
语句,但 TP 函数支持 async/await 我强烈建议您使用它而不是承诺链代码更容易阅读。例如(注,未测试)
/**
* Creates the assets assetB and assetA
* @param {mynamespace.createAssetA} createAssetA - the AssetA to create
* @transaction
*/
async function createAssetA(createAssetA) {
try {
const assetBRegistry = await getAssetRegistry('mynamespace.AssetB');
await assetBRegistry.addAll(createAssetA.assetB);
await addAssetA(createAssetA);
} catch(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
*/
async function addAssetA(createAssetA){
try {
const assetARegistry = await getAssetRegistry('mynamespace.AssetA');
const newAssetA = castcreateAssetATxToAsset(createAssetA);
await assetARegistry.add(newAssetA);
} catch(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){
const factory = getFactory();
// Create a new instance of AssetA class from the mynamespace namespace with the id createAssetA.id
let newAssetA = factory.newResource('mynamespace', 'AssetA', createAssetA.id);
newAssetA.assetAFieldX = createAssetA.assetAFieldX;
newAssetA.assetB = [];
for (let i = 0; i
我目前正在 Hyperledger Composer 中开发交易。此交易将创建 2 个不同的资产:资产 A 和资产 B。 资产 A 有一个类型为资产 B 的字段。所以我首先需要创建资产 B,然后用 link 到资产 B 创建资产 A。经过一些努力,我成功地实现了逻辑。现在同行returns出现以下错误:
2018-10-10T14:06:14.022Z [5ad2a944] DEBUG :NodeDataCollection :add() > assetAIdValue, {"$class": "mynamespace.assetA","assetAId": "assetAIdValue","assetAFieldX": "assetAFieldXValue","assetB": ["resource:mynamespace.assetB#assetBIdValue1", "resource:mynamespace.assetB#assetBIdValue2"]},"$registryType":"Asset","$registryId":"mynamespace.assetA"}, false
(node:17) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Error when creating AssetA with id assetAIdValue
Error: [5ad2a944]No ledger context for GetState. Sending ERROR
这是我的模型
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 服务器可见?
看起来您在承诺链中的代码中缺少视图 return
语句,但 TP 函数支持 async/await 我强烈建议您使用它而不是承诺链代码更容易阅读。例如(注,未测试)
/** * Creates the assets assetB and assetA * @param {mynamespace.createAssetA} createAssetA - the AssetA to create * @transaction */ async function createAssetA(createAssetA) { try { const assetBRegistry = await getAssetRegistry('mynamespace.AssetB'); await assetBRegistry.addAll(createAssetA.assetB); await addAssetA(createAssetA); } catch(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 */ async function addAssetA(createAssetA){ try { const assetARegistry = await getAssetRegistry('mynamespace.AssetA'); const newAssetA = castcreateAssetATxToAsset(createAssetA); await assetARegistry.add(newAssetA); } catch(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){ const factory = getFactory(); // Create a new instance of AssetA class from the mynamespace namespace with the id createAssetA.id let newAssetA = factory.newResource('mynamespace', 'AssetA', createAssetA.id); newAssetA.assetAFieldX = createAssetA.assetAFieldX; newAssetA.assetB = []; for (let i = 0; i