您可以将自v0.20以来的REST
API调用的结果返回给调用的客户端应用程序,因此可以进行如下操作(未经测试,但是您知道了)。注意:您可以直接通过REST直接使用您的参数(或您为自己的业务网络创建的任何端点-
下面的示例是
trade-network)调用REST API结束(/ GET Trader
),而不是使用“只读”的示例如下所述的事务处理器端点,用于将更大的结果集返回到客户端应用程序。在文档中查看有关此内容的更多信息
使用API的NODE JS Client:
const BusinessNetworkConnection = require('composer-client').BusinessNetworkConnection; const rp = require('request-promise'); this.bizNetworkConnection = new BusinessNetworkConnection(); this.cardName ='admin@mynet'; this.businessNetworkIdentifier = 'mynet'; this.bizNetworkConnection.connect(this.cardName) .then((result) => { //You can do ANYTHING HERE eg. }) .catch((error) => { throw error; }); // set up my read only transaction object - find the history of a particular Participant - note it could equally be an Asset instead ! var obj = { "$class": "org.example.trading.MyPartHistory", "tradeId": "P1" }; async function callPartHistory() { var options = { method: 'POST', uri: 'http://localhost:3000/api/MyPartHistory', body: obj, json: true }; let results = await rp(options); // console.log("Return value from REST API is " + results); console.log(" "); console.log(`PARTICIPANT HISTORY for Asset ID: ${results[0].tradeId} is: `); console.log("============================================="); for (const part of results) { console.log(`${part.tradeId} ${part.name}` ); } } // Main callPartHistory();//模型文件
@commit(false)@returns(Trader[])transaction MyPartHistory {o String tradeId}只读交易处理器代码(在“ logic.js”中):
async function participantHistory(tx) { const partId = tx.tradeid; const nativeSupport = tx.nativeSupport; // const partRegistry = await getParticipantRegistry('org.example.trading.Trader') const nativeKey = getNativeAPI().createCompositeKey('Asset:org.example.trading.Trader', [partId]); const iterator = await getNativeAPI().getHistoryForKey(nativeKey); let results = []; let res = {done : false}; while (!res.done) { res = await iterator.next(); if (res && res.value && res.value.value) { let val = res.value.value.toString('utf8'); if (val.length > 0) { console.log("@debug val is " + val ); results.push(JSON.parse(val)); } } if (res && res.done) { try { iterator.close(); } catch (err) { } } } var newArray = []; for (const item of results) { newArray.push(getSerializer().fromJSON(item)); } console.log("@debug the results to be returned are as follows: "); return newArray; // returns something to my NodeJS client (called via REST API)}


