如何通过Hyperledger Composer查询assetStatus为invalid的资产所属Owner?
Hey there, let's work through how to pull the Owners linked to assets marked as "invalid" using Hyperledger Composer. First, let's recap your model definition for context:
namespace org.acme.myNetwork asset Asset identified by assetId { o String assetId --> Owner owner o String assetStatus } participant Owner identified by ownerId { o String ownerId o String ownerName o String ownerStatus }
You're right that basic single-condition examples are common in docs, but Composer's query language absolutely supports the kind of关联 query you need. Here are two straightforward ways to make this work:
Option 1: Directly Query for Owners via Linked Invalid Assets
You can craft a query that targets Owner participants, filtering only those who have at least one asset with an "invalid" status. The exists keyword here is perfect for checking if a matching linked asset exists:
query getOwnersOfInvalidAssets { description: "Fetch all Owners associated with assets that have status 'invalid'" participant org.acme.myNetwork.Owner where ( exists ( SELECT org.acme.myNetwork.Asset WHERE (assetStatus == "invalid" AND owner == this) ) ) }
How this works:
thisrefers to the currentOwnerinstance being evaluated in the queryexistschecks if there's anyAssetlinked to that Owner whereassetStatusis "invalid"- The query returns only those Owners that meet this criteria
Option 2: First Fetch Invalid Assets, Then Extract Owners
If you prefer a two-step approach (maybe you need the invalid assets for other purposes too), you can first query the assets, then pull their linked Owners in your application code:
Step 1: Query for Invalid Assets
First, create a query to get all assets with the "invalid" status:
query getInvalidAssets { description: "Get all assets marked as 'invalid'" asset org.acme.myNetwork.Asset where (assetStatus == "invalid") }
Step 2: Extract Owners in Your Code (JavaScript Example)
Once you have the invalid assets, map over them to get their linked Owners. We'll also add a step to deduplicate in case one Owner has multiple invalid assets:
// Fetch the invalid assets from the network const invalidAssets = await businessNetworkConnection.query('getInvalidAssets'); // Extract the linked Owner from each asset const owners = invalidAssets.map(asset => asset.owner); // Remove duplicate Owners (if needed) const uniqueOwners = [...new Set(owners)];
Quick Tips
- If you want to make the query flexible (e.g., pass in different status values dynamically), replace
"invalid"with a parameter like_$status, then pass the value when you call the query - Always test your queries using the Composer Playground first—it's a great way to verify results before integrating into your app
内容的提问来源于stack exchange,提问作者Keanu

