为什么获取不到资产附属参与者的属性?

Why can't I obtain the attributes of a participant that is attached to an asset?

我想更新资产。为此,我想检查是否只有创建它的用户(此处:发布医生)才能更新它。 问题:我可以得到"itemToUpdate.issueingPhysician",但是"itemToUpdate.issueingPhysician.userId"是未定义的。

为了说明,我在我的代码中放入了一条错误消息(见下文)并在 Composer Playground 中测试了代码。它返回:

"Error: Only the physician who issued the prescription can change it. The current participant is: 1772 But the issueing Physican has userId: undefined The Issueing Physician is:Relationship {id=org.[...].participant.Physician#1772} His name is: undefined"

Fabric 版本是 hlfv12。 出于测试目的,我授予所有参与者管理员权限。

交易的js代码:

/**
* Update a prescription.
* @param {org.[..].UpdatePrescription} transaction
* @transaction
*/
async function processUpdatingOfPrescription(transaction) {
     var prescriptionItemAssetRegistry = await getAssetRegistry('org.[..].Prescription');
     var itemToUpdate = await prescriptionItemAssetRegistry.get(transaction.recordId);
     if (getCurrentParticipant().userId == itemToUpdate.issueingPhysician.userId && itemToUpdate.status !== 'DELETED') {
     // [...]
     }
     else { // only to demonstrate that it is the same user:
         throw new Error('Only the physician who issued the prescription can change it. The current participant is: ' + getCurrentParticipant().userId + 
                    ' But the issueing Physican has userId: ' + itemToUpdate.issueingPhysician.userId + 'The itemToUpdate is:' + itemToUpdate.issueingPhysician)
}

}

cto-File 中的资产:

asset Prescription identified by recordId {
  o String recordId
  o String prescribedDrug
  --> Physician issueingPhysician
 }

cto 文件中的用户(=医师):

participant Physician identified by userId {
  o String userId
  o String firstName
  o String lastName
}

cto-File 中的事务:

transaction UpdatePrescription {
  o String recordId
}

我想获取 "itemToUpdate.issueingPhysician.userId" 的值。

当您检索资产时,您必须自己 'resolve' 关系 (itemToUpdate.issueingPhysician.userId)。在那里查看答案 AssetRegistry.get function is not returning the complete object in Hyperledger Composer for more info and the related issue tracker and status on Composer

对于您的标识符比较 - 最好使用 getIdentifier() 获取每个资源实例的单一标识符,以便正确比较标识符本身:

尝试:

'NS' 以下是 'org.acme' 在我的场景中仅供参考。

async function processUpdatingOfPrescription(transaction) {
     var prescriptionItemAssetRegistry = await getAssetRegistry(NS + '.Prescription');
     var itemToUpdate = await prescriptionItemAssetRegistry.get(transaction.recordId);
     console.log('participant is ' + getCurrentParticipant().getIdentifier() );
     if (getCurrentParticipant().getIdentifier() == itemToUpdate.issueingPhysician.getIdentifier() && itemToUpdate.status !== 'DELETED') {
        console.log("yes they match !! ");
     }
     else { // only to demonstrate that it is the same user:
         throw new Error('Only the physician who issued the prescription can change it. The current participant is: ' + getCurrentParticipant().getIdentifier() + 
                    ' But the issueing Physican has userId: ' + itemToUpdate.issueingPhysician.getIdentifier() + 'The itemToUpdate is:' + itemToUpdate.issueingPhysician);
     }
}