如果查询有多个输入,如何在Hyperledger composer logic.js中使用查询?

问题描述 投票:2回答:1

用于查询logic.js我可以使用

await query('selectCommoditiesWithHighQuantity')

但是,如果我有多个输入,我怎么能这样做呢?如果查询具有这样的功能

query selectCommoditiesByTimeAndOwnerAndDataType {
  description: "Select all commodities based on their sender country"
  statement:
      SELECT org.stock.mynetwork.Commodity
          WHERE(time  > _$from AND time < _$to AND owner == _$owner AND dataType == _$dataType)
}

如何从js端调用该查询?

编辑:js代码

/**
 * Track the trade of a commodity from one trader to another
 * @param {org.stock.mynetwork.Receive} receive - the receive to be processed
 * @transaction
 */
async function receiveCommodity(receive) {

    let q1 = await buildQuery('SELECT org.stock.mynetwork.Commodity ' +
                                                'WHERE (productName == _$productName AND owner == _$owner)');


let result2 = await query(q1,{productName:receive.productName,owner: receive.newOwner});
}

let result2 = await query(q1,{productName:receive.productName,owner: receive.newOwner});部分存在问题。如果我只是使用productName: receive.productName它完美的工作,但当我添加owner: receive.newOwner它需要serialize.json

javascript hyperledger-composer
1个回答
1
投票

所以你可以在.qry文件中编写一个查询并调用它,但我不建议这样做。您可以直接从SDK和logic.js文件中进行相同的查询。这背后的推理是,比如几天之后,你想要添加一个按特定值查询的新API,如果你依赖.qry文件(可以工作),那么你需要部署一个新版本的智能联系人,如果您使用SDK,则可以在API中进行更改并尽快部署新的应用程序服务器。

async function someTransaction(receive) {
  let assetRegistry = await getAssetRegistry('YOUR_NAME_SPACE');
  let ownerRegistry = await getParticipantRegistry('YOUR_NAME_SPACE');

  let statement = 'SELECT NAME_SPACE_OF_ASSET WHERE (owner == _$owner && dataType == _$dataType)';
  let qry = buildQuery(statement);

  // This query can be done in different ways
  // assuming newOwner is a string (id of participant)
  let allAssets = await query(qry, { owner: receive.newOwner, dataType: receive.dataType });

  // assuming newOwner is a participant
  let allAssets = await query(qry, { owner: receive.newOwner.getIdentifier(), dataType: receive.dataType });

  if (allAssets.length === 0) {
    // No assets exists, add one
    // use assetRegistry.add()
  } else {
    for (var i = 0; i < allAssets.length; i++) {
      // Iterate over assets belonging to an owner of a product type
      // Do whatever here
      // use assetRegistry.update()
    };
  };

};
© www.soinside.com 2019 - 2024. All rights reserved.