我想更新资产。为此,我想检查只有创建它的用户(在这里:issueingPhysician)可以更新它。 问题:我可以获取“ 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"
结构版本为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文件中的资产:
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文件中的事务:
transaction UpdatePrescription {
o String recordId
}
我想获取“ itemToUpdate.issueingPhysician.userId”的值。
答案 0 :(得分:1)
检索资产时,您必须自己“解决”关系(itemToUpdate.issueingPhysician.userId
)。有关更多信息,请参见答案AssetRegistry.get function is not returning the complete object in Hyperledger Composer以及相关的问题跟踪工具和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);
}
}