作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我可以使用 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/
我是一名优秀的程序员,十分优秀!