首页 > 解决方案 > 在 Hyperledger Composer 中使用关系进行查询

问题描述

我在 Hyperledger Composer 中进行了查询,在该查询中尝试搜索借款人的所有发票。借款人是发票资产的参与者:

asset Invoice identified by invoiceId {
  o String invoiceId
  o String invoiceRef optional
  o DateTime dateCreated
  o String type
  o DateTime invoiceOrPurchaseDate optional
  o Double amount
  o DateTime invoiceDueDate optional
  o String paymentStatus optional
  o Boolean overdue
  o Double outstandingBalance
  --> Participant borrower
  --> Participant lender
}

我需要一个查询来返回借款人的所有发票,我在 Hyperledger 作曲家中通过以下编码做到了这一点:

query QInvoiceByBorrower {
    description: "Select invoice by borrower"
    statement:
        SELECT org.n.blockchaindemo.Invoice
            WHERE (_$borrower == borrower)
}

但是当我尝试通过 REST API 调用查询时,我得到 [] 空结果如下:

http://10.10.4.244:3000/api/queries/QInvoiceByBorrower?borrower=resource:org.n.blockchaindemo.User#1381991

我可以知道如何在 Hyperledger Composer 中创建一个将使用外部关系进行搜索的查询吗?

标签: hyperledger-fabrichyperledgerhyperledger-composer

解决方案


在您的发票定义中,您需要参考您的特定参与者类型,即用户而不是系统类型参与者。因此,发票的最后一部分将是:

  o Double outstandingBalance
  --> User borrower
  --> User lender
 }

通过此更改,您的查询应该可以工作。一个有用的诊断方法是创建一个不带where子句的重复查询,然后检查返回的数据以帮助了解用于查询的参数。

(通过使用 Participant,我认为您的资产将在首次使用资产类型时包含模型中的第一个参与者类型 - 这听起来可能会产生一些意想不到的行为,因此指定实际的参与者类型是有意义的。)


推荐阅读