如何使用查询或JS从Hyperledger Composer提取唯一数据

时间:2019-02-15 09:14:19

标签: hyperledger-composer

我想从Hyperledger Composer的重复数据中获取唯一数据。我该如何实现?

假设我有这样的数据

     [
{
 "$class": "org.stock.mynetwork.Commodity",
 "time": "1",
 "dataType" : "in",
 "productName": "A",
 "quantity": 1,
 "country": "Unknown",
 "owner": "GAGA"
},
{
 "$class": "org.stock.mynetwork.Commodity",
 "time": "2",
 "dataType": "in",
 "productName": “A",
 "quantity": 1,
 "country": "Unknown",
 "owner": "BABA"
},
{
 "$class": "org.stock.mynetwork.Commodity",
 "time": "3",
 "dataType": "out",
 "productName": "C",
 "quantity": 1,
 "country": "Unknown",
 "owner": "GAGA"
},
{
 "$class": "org.stock.mynetwork.Commodity",
 "time": "4",
 "dataType": "in",
 "productName": "C",
 "quantity": 1,
 "country": "Unknown",
 "owner": "GAGA"
},
{
 "$class": "org.stock.mynetwork.Commodity",
 "time": "5",
 "dataType": "out",
 "productName": "B",
 "quantity": 1,
 "country": "Unknown",
 "owner": "BABA"
}
] 

我想要得到的是唯一产品名称的列表。

所以我想要的结果可能是这样的

 [
{
 "$class": "org.stock.mynetwork.Commodity",
 "time": "1",
 "dataType" : "in",
 "productName": "A",
 "quantity": 1,
 "country": "Unknown",
 "owner": "GAGA"
},
{
 "$class": "org.stock.mynetwork.Commodity",
 "time": "3",
 "dataType": "out",
 "productName": "C",
 "quantity": 1,
 "country": "Unknown",
 "owner": "GAGA"
},
{
 "$class": "org.stock.mynetwork.Commodity",
 "time": "5",
 "dataType": "out",
 "productName": "B",
 "quantity": 1,
 "country": "Unknown",
 "owner": "BABA"
}
] 

我不太在意序列。我只想知道我的数据库中有什么productName。

我认为这与logic.js部分有关。我可以使用post方法进行查询并获取数据。但是我不知道如何使用逻辑.js

我不知道如何从logic.js获取数据。官方页面上的示例仅删除或交易。他们不会返回新数据。

感谢您的帮助。我想知道如何使用js发送数据。

1 个答案:

答案 0 :(得分:0)

从数据中获取唯一产品名称的js代码为

const data =[
{
 "$class": "org.stock.mynetwork.Commodity",
 "time": "1",
 "dataType" : "in",
 "productName": "A",
 "quantity": 1,
 "country": "Unknown",
 "owner": "GAGA"
},
{
 "$class": "org.stock.mynetwork.Commodity",
 "time": "3",
 "dataType": "out",
 "productName": "A",
 "quantity": 1,
 "country": "Unknown",
 "owner": "GAGA"
},
{
 "$class": "org.stock.mynetwork.Commodity",
 "time": "5",
 "dataType": "out",
 "productName": "B",
 "quantity": 1,
 "country": "Unknown",
 "owner": "BABA"
}
];

const distinctProductName = [...new Set(data.map(x => x.productName))];

您将在distinctProductName中使用不同的ProductName,但是您将没有产品的详细信息。从您的问题来看,它也没有必要。如果您也需要详细信息,可以执行类似的操作

const data =[
{
 "$class": "org.stock.mynetwork.Commodity",
 "time": "1",
 "dataType" : "in",
 "productName": "A",
 "quantity": 1,
 "country": "Unknown",
 "owner": "GAGA"
},
{
 "$class": "org.stock.mynetwork.Commodity",
 "time": "3",
 "dataType": "out",
 "productName": "A",
 "quantity": 1,
 "country": "Unknown",
 "owner": "GAGA"
},
{
 "$class": "org.stock.mynetwork.Commodity",
 "time": "5",
 "dataType": "out",
 "productName": "B",
 "quantity": 1,
 "country": "Unknown",
 "owner": "BABA"
}
];

const result = Array.from(new Set(data.map(x => x.productName)))
                    .map(productName => {
                             return{
                                    productName: productName,
                                    dataType : data.find(x => x.productName ===productName).dataType
                                   //you can obtain other values as I obtained dataType
                                   };
                              });

您将在result中存储数据。