Hyperledger Fabric交易提交成功但无法获取创建对象(返回Undefined)
Hey there! I know it’s frustrating when your transaction goes through successfully but you don’t get the created object back as expected. Let’s break down the most likely causes and fixes for this issue:
1. Your Chaincode Transaction Function Isn’t Returning the Created Asset
This is the most common culprit. Hyperledger Fabric doesn’t automatically return the created object—your chaincode needs to explicitly send it back.
For example, if your chaincode looks like this (missing the return statement):
async function createMyAsset(ctx, assetDetails) { const asset = JSON.parse(assetDetails); await ctx.stub.putState(asset.id, Buffer.from(JSON.stringify(asset))); // No return here! }
Your client will get undefined even though the asset was saved to the world state.
Fix: Update your chaincode to return the created asset after saving it:
async function createMyAsset(ctx, assetDetails) { const asset = JSON.parse(assetDetails); await ctx.stub.putState(asset.id, Buffer.from(JSON.stringify(asset))); // Return the asset data return JSON.stringify(asset); }
2. Check Your Transaction Definition in the Business Network CTO
If you’re using a Business Network Definition, make sure your transaction is defined to return the asset type.
In your .cto file, ensure the transaction specifies a return type:
namespace your.namespace asset MyAsset identified by id { o String id o String name } transaction CreateMyAsset { o String id o String name --> MyAsset createdAsset // Declares the return asset type }
Without this, even if your chaincode returns data, the client might not map it correctly to the asset object.
3. Debug the Return Value in Your Client Code
Add logging right after the submitTransaction call to confirm what’s actually being returned. Modify your code like this:
async submit(resource, method) { try{ this.businessNetworkDefinition = await this.bizNetworkConnection.connect(cardname); if (!this.businessNetworkDefinition) { console.log("Error in network connection"); throw "Error in network connection"; } let factory = this.businessNetworkDefinition.getFactory(); let transaction = factory.newTransaction(NS, method); Object.assign(transaction, resource); // Add logging to inspect the result const transactionResult = await this.bizNetworkConnection.submitTransaction(transaction); console.log('Raw transaction result:', transactionResult); // Parse if the result is JSON string return transactionResult ? JSON.parse(transactionResult) : transactionResult; }catch(error){ console.log(error); throw error; } }
If the log shows undefined, the issue is definitely with the chaincode not returning data. If it shows raw JSON, parsing it will give you the expected asset object.
4. Verify Transaction Parameter Mapping
Double-check that Object.assign(transaction, resource) is correctly populating all required fields for the transaction. Even if the transaction succeeds, missing or misaligned fields could lead to unexpected return behavior (though you confirmed the asset is created, it’s still worth ruling out).
内容的提问来源于stack exchange,提问作者Marcus Ataide

