如何通过rest api从Hyperledger中的数据源中通过id查找模型实例

时间:2018-04-08 07:46:18

标签: hyperledger-fabric hyperledger-composer

我已经在Hyperledger composer脚本文件中编写了我的事务,如下所示,并通过composer部署为rest api。交易成功写入区块链,我可以通过以下获取电话获取所有交易。

http://localhost:3000/api/org.company.dtrace.GroserInformation

以上电话的结果

[
{
"$class": "org.company.dtrace.GroserInformation",
"asset": "resource:org.company.dtrace.SampleAsset#6717",
"newValue": "6717",
"GroserID": "6717",
"GroserInfo": "6717",
"SeedsInfo": "6717",
"Pest_Management": false,
"Nutritional_Check": false,
"Harvest_Date": "2018-04-08T07:24:58.645Z",
"Package_Date": "2018-04-08T07:24:58.645Z",
"Batch_Number": 0,
"Dispatched_On": "2018-04-08T07:24:58.645Z",
"Transportation": "6717",
"Destination": "6717",
"Best_Within": "6717",
"consumerRegion": "6717",
"transactionId": "0260f66b7ff0e8520b3195a6c7fcd8d8b4111162ed0ed0d818b4142bd35b694c",
"timestamp": "2018-04-08T07:26:36.836Z"
},
{
"$class": "org.company.dtrace.GroserInformation",
"asset": "resource:org.company.dtrace.SampleAsset#8864",
"newValue": "8864",
"GroserID": "8864",
"GroserInfo": "8864Info",
"SeedsInfo": "8864",
"Pest_Management": false,
"Nutritional_Check": false,
"Harvest_Date": "2018-04-07T14:10:50.677Z",
"Package_Date": "2018-04-07T14:10:50.677Z",
"Batch_Number": 0,
"Dispatched_On": "2018-04-07T14:10:50.677Z",
"Transportation": "8864",
"Destination": "8864",
"Best_Within": "8864",
"consumerRegion": "8864",
"transactionId": "22f63084b579023cad40a3dd30143e5fffe76725879af020ead3cc63fa1679b3",
"timestamp": "2018-04-07T14:16:19.086Z"
}
]

但是如何通过ID获取特定的交易详情,因为我看到错误,如下面的Rest api

休息电话:  http://localhost:3000/api/org.company.dtrace.GroserInformation/6717

错误:

{
"error": {
"statusCode": 404,
"name": "Error",
"message": "Unknown \"org_company_dtrace_GroserInformation\" id \"6717\".",
"status": 404,
"code": "MODEL_NOT_FOUND",
"stack": "Error: Unknown \"org_company_dtrace_GroserInformation\" id \"6717\".\n    at Function.convertNullToNotFoundError (/usr/local/lib/node_modules/composer-rest-server/node_modules/loopback/lib/persisted-model.js:89:17)\n    at invokeRestAfter (/usr/local/lib/node_modules/composer-rest-server/node_modules/strong-remoting/lib/rest-adapter.js:531:25)\n    at /usr/local/lib/node_modules/composer-rest-server/node_modules/async/dist/async.js:3866:24\n    at replenish (/usr/local/lib/node_modules/composer-rest-server/node_modules/async/dist/async.js:998:17)\n    at iterateeCallback (/usr/local/lib/node_modules/composer-rest-server/node_modules/async/dist/async.js:983:17)\n    at /usr/local/lib/node_modules/composer-rest-server/node_modules/async/dist/async.js:958:16\n    at /usr/local/lib/node_modules/composer-rest-server/node_modules/async/dist/async.js:3871:13\n    at interceptInvocationErrors (/usr/local/lib/node_modules/composer-rest-server/node_modules/strong-remoting/lib/remote-objects.js:713:22)\n    at /usr/local/lib/node_modules/composer-rest-server/node_modules/strong-remoting/node_modules/loopback-phase/node_modules/async/lib/async.js:154:25\n    at /usr/local/lib/node_modules/composer-rest-server/node_modules/strong-remoting/node_modules/loopback-phase/node_modules/async/lib/async.js:154:25\n    at /usr/local/lib/node_modules/composer-rest-server/node_modules/strong-remoting/node_modules/loopback-phase/node_modules/async/lib/async.js:154:25\n    at execStack (/usr/local/lib/node_modules/composer-rest-server/node_modules/strong-remoting/lib/remote-objects.js:522:7)\n    at RemoteObjects.execHooks (/usr/local/lib/node_modules/composer-rest-server/node_modules/strong-remoting/lib/remote-objects.js:526:10)\n    at phaseAfterInvoke (/usr/local/lib/node_modules/composer-rest-server/node_modules/strong-remoting/lib/remote-objects.js:684:10)\n    at runHandler (/usr/local/lib/node_modules/composer-rest-server/node_modules/strong-remoting/node_modules/loopback-phase/lib/phase.js:135:5)\n    at iterate (/usr/local/lib/node_modules/composer-rest-server/node_modules/strong-remoting/node_modules/loopback-phase/node_modules/async/lib/async.js:146:13)"

}    }

lib/script.js

 /**
 * Sample transaction processor function.
 * @param {org.company.dtrace.GroserInformation} tx The sample 
 transaction instance.
 * @transaction
 */
 function GroserInformation(tx) {

// Save the old value of the asset.
var oldValue = tx.asset.value;

// Update the asset with the new value.
tx.asset.value = tx.newValue;

// Get the asset registry for the asset.
return getAssetRegistry('org.company.dtrace.SampleAsset')
    .then(function (assetRegistry) {

        // Update the asset in the asset registry.
        return assetRegistry.update(tx.asset);

    })
    .then(function () {

        // Emit an event for the modified asset.
        var event = getFactory().newEvent('org.company.dtrace', 'SampleEvent');
        event.asset = tx.asset;
        event.oldValue = oldValue;
        event.newValue = tx.newValue;
        emit(event);

    });

}

model / sample.cto file

 transaction GroserInformation {
 --> SampleAsset asset
o String newValue
o String GroserID
o String GroserInfo
o String SeedsInfo
o Boolean Pest_Management
o Boolean Nutritional_Check
o DateTime Harvest_Date
o DateTime Package_Date
o Integer Batch_Number
o DateTime Dispatched_On
o String Transportation
o String Destination
o String Best_Within
o String consumerRegion
}

1 个答案:

答案 0 :(得分:0)

6717是资产的ID,而不是交易。对于交易,请使用0260f66b7ff0e8520b3195a6c7fcd8d8b4111162ed0ed0d818b4142bd35b694c

资产:

http://localhost:3000/api/org.company.dtrace.SampleAsset/6717

交易:

http://localhost:3000/api/org.company.dtrace.GroserInformation/0260f66b7ff0e8520b3195a6c7fcd8d8b4111162ed0ed0d818b4142bd35b694c