hyperledger composer:无法访问从查询返回的对象的属性

hyperledger composer: not able to access attributes of object returned from query

在我的 hyperledger composer 应用程序的交易处理器功能中,我使用了以下代码行(为了让患者使用电子邮件地址 'adam@gmail.com'):

let result = await query('selectPatientByEmail', {
    "email": "adam@gmail.com"    
});

查询在queries.qry文件中定义如下:

query selectPatientByEmail {
    description: "Select the patient with the given email address"
    statement:
        SELECT org.comp.app.Patient
            WHERE (email == _$email) }

Patient 在model.cto 文件中定义如下:

participant Patient identified by id {
  o String id
  o String firstName
  o String lastName
  o String email
}

问题是这样的:当我尝试访问返回患者的 ID 时,这是不可能的。即result.id是"undefined"

如何获取返回患者的 ID?


此问题与以下问题相关: how to define BusinessNetworkConnection in hyperledger-composer transaction processor?

您为其定义的参与者 Patient 没有名称为 email 的属性。因此,查询 selectPatientByEmail 将始终 return Empty Object {}。当您调用 result.id 相当于 {}.id 时,它将是未定义的

请记住,结果将是一个 JS 对象数组。

因此,您可以使用 for 循环遍历结果 - 然后您可以访问对象的任何属性(根据您的模型),例如

let result = await query('selectPatientByEmail', {
    "email": "adam@gmail.com"    
    });

for (var n = 0; n < result.length; n++) {

       console.log("Patient Id is " + result[n].id );
       // alternative : console.log("Patient Id is " + result[n].getIdentifier());
       console.log("Patient Email is " + result[n].email);
  }