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



在我的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) }

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

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

问题是:当我试图访问返回患者的id时,这是不可能的。也就是说,result.id是"未定义的">

如何访问返回患者的id?


此问题与以下问题有关:如何在hyperledger composer事务处理器中定义BusinessNetworkConnection?

请记住,结果将是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);
}

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

最新更新