2

for querying in logic.js I can use

await query('selectCommoditiesWithHighQuantity')

But how can I do that if I have multiple input? if the query have function like this

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)
}

how can I call that query from js side?

edit: js code

/**
 * 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});
}

there is a problem with let result2 = await query(q1,{productName:receive.productName,owner: receive.newOwner}); part. If I just use productName: receive.productName it works perfectly, but when I add owner: receive.newOwner it need serialize.json

Darwin Harianto
  • 435
  • 4
  • 15
  • so it seems we have also been chatting on hyperledger.chat :). Didn't realize it was you. So if the error is on `newOwner`, we will have to check its type. Can you post the relevant part of `Receive` transaction. Is `newOwner` a `string` or a participant? That can cause a serialize json error – Varun Agarwal Feb 25 '19 at 08:31
  • newOwner is a participant – Darwin Harianto Feb 25 '19 at 08:33
  • ok, just updated the code This will solve the problem – Varun Agarwal Feb 25 '19 at 08:52

1 Answers1

1

So you can write a query inside the .qry file and call it, but I do not recommend doing that. You can make the same queries directly from the SDK and in the logic.js file. Reasoning behind this is, say a few days later, you want to add a new API that queries by a certain value, if you rely on the .qry file (which will work), then you will need to deploy a new version of smart contact, whereas if you use the SDK, you can do a change in the API and deploy a new application server asap.

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()
    };
  };

};
Varun Agarwal
  • 1,587
  • 14
  • 29
  • thank you for replying, I have some question. is this implementation on node sdk side? how can I use this on logic.js? I am really new on this side. Thank you for showing the rope – Darwin Harianto Feb 25 '19 at 01:09
  • only difference in nodejs sdk side and logic.js side is the term `businessNetworkConnection`. Simply add/ remove the term on all API calls to convert code between nodejs and logic.js, try it on your own. If you don't get it, I will add another snippet. So you would use `const qry = buildQuery(statement);` in `logic.js` – Varun Agarwal Feb 25 '19 at 06:34
  • okay, tried to use it. my code is > 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}); but when I called it, it gives me error of jsonserializer – Darwin Harianto Feb 25 '19 at 07:52
  • Your code is fine, you don't need to use `await` in the `buildQuery()` part. Th error might be coming from somewhere else, how are you getting the value of `receive`? Could you post the above part of the code as well? – Varun Agarwal Feb 25 '19 at 08:23