You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hyperledger Fabric Node.js链码继承问题:调用AdminContract类中getLastPatientId函数提示不存在

Hey there, let's break down why you're hitting this "function does not exist" error when calling getLastPatientId from your AdminContract—it boils down to how Hyperledger Fabric handles multiple contract classes and a couple of small code mistakes in your implementation.

Core Issue: Unnamed Contracts & Default Contract Resolution

When you export multiple contract classes (like CommonContract and AdminContract) in your index.js, Fabric treats each as a named contract. By default, Fabric uses the lowercase version of your class name as the contract identifier (e.g., CommonContract becomes commoncontract, AdminContract becomes admincontract).

When you call network.getContract('basic') in your API, you're only specifying the chaincode name (basic) but not the target contract name. Fabric will fall back to using the first registered contract (your CommonContract) as the default—so it can't see the methods defined in AdminContract at all.

Second Issue: Broken Parent Method Call

In your AdminContract's getLastPatientId function, you're calling getAllPatient(ctx) directly as a global function, but it's an instance method inherited from CommonContract. This would throw a separate error once you fix the contract resolution issue.

Fix Steps

1. Explicitly Name Your Contracts (Recommended)

Add constructors to both contract classes to define clear, readable contract names. This avoids relying on Fabric's default naming behavior:

CommonContract.js

'use strict';
const { Contract } = require('fabric-contract-api');
class CommonContract extends Contract {
    // Add constructor to set contract name
    constructor() {
        super('common'); // Name this contract "common"
    }

    // ... your existing initLedger, getPatient, getAllPatient methods
}
module.exports = CommonContract;

AdminContract.js

'use strict';
let Patient = require('./PatientAssets.js');
const CommonContract = require('./CommonContract.js');
class AdminContract extends CommonContract {
    constructor() {
        super('admin'); // Name this contract "admin"
    }

    async getLastPatientId(ctx) {
        // Fix: Call the inherited method using `this`
        let result = await this.getAllPatient(ctx);
        // Parse the JSON string returned by getAllPatient
        let parsedPatients = JSON.parse(result);
        // Fix typo: "patiendId" → plus, your data uses "Key" for patient IDs like "PID0"
        return parsedPatients[parsedPatients.length - 1].Key;
    }
}
module.exports = AdminContract;

2. Target the Correct Contract in Your API Call

Update your admin.js to specify both the chaincode name and the target contract name when fetching the contract instance:

// Replace this line:
// const contract = network.getContract('basic');
// With this (specify the "admin" contract):
const contract = network.getContract('basic', 'admin');

3. Clean Up Minor Typos/Logic

  • In AdminContract, you had a typo patiendId → corrected to reference the actual Key field from your patient data (since your stored keys are PID0, PID1, etc.)
  • getAllPatient returns a JSON string, so we added JSON.parse to convert it to a usable object before accessing the last entry.

Why Parent Class Methods Worked

Since you didn't specify a contract name in your original API call, Fabric defaulted to using the first registered contract (CommonContract), which is why its methods like getAllPatient worked fine. The AdminContract methods were invisible to this default context.

Verify the Fix

  1. Repackage and reinstall your chaincode on the Fabric network
  2. Restart your Node.js API server
  3. Call your /getAllPatient endpoint again—this time it should successfully resolve and execute getLastPatientId from the AdminContract.

内容的提问来源于stack exchange,提问作者Sushant Barje

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 11:07:31