用于在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
答案 0 :(得分:1)
因此,您可以在.qry
文件内编写查询并调用它,但是我不建议这样做。您可以直接从SDK和logic.js
文件中进行相同的查询。这样做的理由是,例如几天后,您想要添加一个按一定值查询的新API,如果您依靠.qry
文件(将起作用),则需要部署一个新API。版本的智能联系人,而如果使用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()
};
};
};