I want to update an asset. For that, I want to check that only the user who created it (here: the issueingPhysician) can update it. The problem: I can get "itemToUpdate.issueingPhysician", but "itemToUpdate.issueingPhysician.userId" is undefined.
To illustrate, I put an error message into my code (see below) and tested the code in Composer Playground. It returned:
"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"
The Fabric Version is hlfv12. For testing purposes, I gave all participants admin rights.
The js-Code for the transaction:
/**
* 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)
}
}
The asset in the cto-File:
asset Prescription identified by recordId {
o String recordId
o String prescribedDrug
--> Physician issueingPhysician
}
The user (=physician) In the cto-File:
participant Physician identified by userId {
o String userId
o String firstName
o String lastName
}
The transaction in the cto-File:
transaction UpdatePrescription {
o String recordId
}
I would like to get a value for "itemToUpdate.issueingPhysician.userId".
when you retrieve an Asset you have to 'resolve' the relationship (itemToUpdate.issueingPhysician.userId
) yourself. See the answer AssetRegistry.get function is not returning the complete object in Hyperledger Composer for more info and the related issue tracker and status on Composer there.
For your identifier comparison - best to use getIdentifier()
to get the singular identifier of each resource instance, for correct comparison of the identifiers themselves:
try:
'NS' below is 'org.acme' in my scenario FYI.
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);
}
}