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

时间:2018-07-29 22:35:04

标签: hyperledger-composer

在我的超级账本撰写程序应用程序中的事务处理程序功能中,我使用了以下代码行(以使患者的电子邮件地址为“ adam@gmail.com”):

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

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

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

在model.cto文件中,患者定义如下:

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

问题是这样的:当我尝试访问返回患者的ID时,这是不可能的。也就是说,result.id为“未定义”

如何获取返回患者的身份证?


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

2 个答案:

答案 0 :(得分:0)

您已定义其参加者的患者患者没有名称为电子邮件的属性。因此,查询selectPatientByEmail将始终返回空对象{} 。调用result.id等同于 {}。id 时,它将是未定义的

答案 1 :(得分:0)

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

因此,您可以使用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); } 循环浏览结果-然后您可以访问对象的任何属性(根据模型),例如。

onchange