gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 23:05:47 25 4
gpt4 key购买 nike

我可以使用 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

最佳答案

因此您可以在.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()
};
};

};

关于javascript - 如果查询有多个输入,如何在 Hyperledger composer logic.js 中使用查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54803014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com