如何正确调用selectCommoditiesByOwner查询?求可用调用示例
selectCommoditiesByOwner query with owner parameter I've run into this exact issue before with Hyperledger Composer queries—when dealing with resource-type parameters like owner, you need to pass the full resource URI for the query to match correctly. Let me break down the working examples for different scenarios:
1. Calling the query in a transaction processor (JavaScript)
If you're executing this query inside a transaction function, the cleanest way is to use the resource object's built-in toURI() method, which generates the properly formatted string the query expects:
/** * Sample transaction handler * @param {org.acme.biznet.YourSampleTransaction} tx The incoming transaction * @transaction */ async function handleSampleTransaction(tx) { // Assume your transaction includes an 'owner' reference to a Participant const targetOwner = tx.owner; // Set up query parameters with the full resource URI const queryArgs = { owner: targetOwner.toURI() }; // Execute the query const matchingCommodities = await query('selectCommoditiesByOwner', queryArgs); // Work with the results console.log(`Retrieved ${matchingCommodities.length} commodities for ${targetOwner.email}`); // Add your business logic here }
If you're fetching the owner from a registry instead of getting it from the transaction, you can also manually construct the URI:
// Get the participant registry first const participantRegistry = await getParticipantRegistry('org.acme.biznet.Participant'); const targetOwner = await participantRegistry.get('john-doe-123'); // Use the owner's ID // Manually build the resource URI const queryArgs = { owner: `resource:${targetOwner.getFullyQualifiedIdentifier()}` }; const matchingCommodities = await query('selectCommoditiesByOwner', queryArgs);
2. Calling the query via REST API
If you're using the auto-generated Composer REST API, you need to pass the full resource URI as the parameter value:
GET Request
Encode the URI and append it as a query parameter:
GET /api/queries/selectCommoditiesByOwner?owner=resource%3Aorg.acme.biznet.Participant%23john-doe-123
(Note: resource:org.acme.biznet.Participant#john-doe-123 is URL-encoded here)
POST Request
Send the parameter in a JSON body:
{ "owner": "resource:org.acme.biznet.Participant#john-doe-123" }
Why your previous attempts didn't work
Let's quickly cover why your earlier tries failed:
- Passing the
ownerobject directly: Queries expect string values for resource parameters, not the object itself. owner.getFullyQualifiedIdentifier(): This returns the identifier without theresource:prefix, which the query needs to recognize it as a valid resource reference.- If
"resource:" + owner.getFullyQualifiedIdentifier()didn't work, double-check that the owner's fully qualified identifier includes the correct type (e.g.,org.acme.biznet.Participant#john-doe-123—make sure the#and ID are present).
内容的提问来源于stack exchange,提问作者PPCM

