Hyperledger事务执行报错:指定查询不存在问题求助
Problem Context
I've defined a Cashier participant in my Hyperledger business network model:
namespace com.emastersapp /** * Cashier participant identified by userId */ participant Cashier identified by userId { o String userId }
And created a query in queries.qry:
query getCashierByUserId { description: "Get Cashier by UserId" statement: SELECT com.emastersapp.Cashier WHERE (userId == _$userId) }
When calling this query in logic.js:
let results = await query('getCashierByUserId', { userId: resource.userId });
I get this error during transaction execution:
"Error trying invoke business network. Error: No valid responses from any peers.\nResponse from attempted peer comms was an error: Error: 2 UNKNOWN: error executing chaincode: transaction returned with failure: Error: The specified query does not exist"
Troubleshooting & Solutions
Let's break down the most common causes and fixes for this error:
Mismatched query name or namespace issues
Double-check that the query name you're calling inlogic.js(getCashierByUserId) exactly matches the name defined inqueries.qry—this includes case sensitivity. Hyperledger doesn't ignore uppercase/lowercase differences here, so a typo likegetcashierbyuseridwill fail. Also confirm the namespace in the query statement (com.emastersapp.Cashier) matches your participant's namespace from the CTO file.Query file not included in the business network archive (BNA)
It's easy to forget to repackage your business network after updatingqueries.qry. If the query wasn't included in the BNA you deployed, the chaincode won't recognize it. Follow these steps to refresh your deployment:- Stop the running network:
composer network stop -n your-network-name -c admin@your-network-name - Repackage the network:
composer archive create -t dir -n . - Reinstall the network:
composer network install -c PeerAdmin@hlfv1 -a your-network@x.x.x.bna - Restart the network:
composer network start -n your-network-name -c PeerAdmin@hlfv1 -V x.x.x -A admin -S adminpw - Import the updated admin card:
composer card import -f admin@your-network-name.card
Make sure
queries.qrylives in the same directory as yourmodel.ctoandlogic.js—this is where the packaging tool expects it to be.- Stop the running network:
Stale chaincode instance
Sometimes Fabric caches old chaincode instances, so even after repackaging, the peer might still be using an outdated version without your new query. Try deleting the old chaincode container (via Docker commands) and restarting the network. Alternatively, use the upgrade command to force a refresh:composer network upgrade -n your-network-name -c PeerAdmin@hlfv1 -V x.x.xSyntax errors in the query definition
A hidden syntax mistake inqueries.qrycan prevent the query from being registered. Double-check:- The
SELECTstatement uses the correct namespace and participant name - The parameter syntax (
_$userId) is correct (leading underscore + dollar sign) - No typos, missing parentheses, or invalid characters (like curly quotes instead of straight quotes)
- The
内容的提问来源于stack exchange,提问作者Marcus Ataide

