首页 > 解决方案 > 为什么后续 HLC 查询返回未定义?

问题描述

我想通过其 ID 检索参与者并将详细信息记录到控制台。我已经使用命名作曲家查询来检索参与者。访问控制是自动生成的(默认)。

模型文件为:

namespace org.example.empty

participant SampleParticipant identified by partId {
  o String partId
  o String LName
  o String FName  
}
transaction getSampleParticipant {
  o String partyId
}

查询.qry 文件:

query selectParticipantById {
  description: "Select participant by Id"
  statement: 
    SELECT org.example.empty.SampleParticipant  
    WHERE (partId == _$pId)
}

事务处理器是:

/**
 * get a participant.
 * @param {org.example.empty.getSampleParticipant} txgetParty 
 * @transaction
 */

async function getParty (txgetParty) {
  const queryResult = await query ('selectParticipantById', {pId: txgetParty.partyId})  
  console.log('Participant detail.')
  console.log(queryResult.partId)
  console.log(queryResult.FName)
  console.log(queryResult.LName)
}

在 Composer 操场上安装后,我创建了一个 SampleParticipant 并提交 getSampleParticipant 事务。控制台输出三遍“未定义”。我错过了什么?

标签: hyperledger-composer

解决方案


我发现查询返回一个对象数组。因此,使用for循环访问数组元素将解决问题。

    for (let index= 0; index< queryResult.length; n++) {

         console.log("partId: " + queryResult[index].partId );
         console.log("First Name: " + queryResult[index].FName);
         console.log("Last Name: " + queryResult[index].LName);
      }

推荐阅读