导入composer-common时FileSystemCardStore显示undefined问题排查
问题分析与解决方案
首先,你遇到的FileSystemCardStore返回undefined的核心原因是0.19.x版本的composer-common并没有将这个类作为根模块的导出成员,直接通过require('composer-common').FileSystemCardStore自然找不到对应的类。
1. 正确的导入方式
在Hyperledger Composer 0.19.x中,FileSystemCardStore位于composer-common/lib/cardstore子目录下,需要直接引用具体的模块文件来导入:
// 正确的导入语句 const FileSystemCardStore = require('composer-common/lib/cardstore/FileSystemCardStore').FileSystemCardStore;
导入后你可以打印验证:
console.log('------>', FileSystemCardStore); // 此时应该输出对应的类构造函数,而非undefined
2. 满足「创建新身份Card」需求的完整流程
既然你的目标是为新身份创建Card,这里提供一个完整的示例流程,包含身份发行、Card创建与存储的关键步骤:
const BusinessNetworkConnection = require('composer-client').BusinessNetworkConnection; const FileSystemCardStore = require('composer-common/lib/cardstore/FileSystemCardStore').FileSystemCardStore; const Card = require('composer-common').Card; async function createAndSaveNewCard() { try { // 1. 连接到已部署的业务网络(使用管理员Card) const adminConnection = new BusinessNetworkConnection(); await adminConnection.connect('admin@your-business-network'); // 2. 为目标参与者发行新身份 // 替换为你实际的参与者ID和身份标签 const participantId = 'org.example.User#user001'; const identityLabel = 'user001-identity'; const identityIssueResponse = await adminConnection.issueIdentity(participantId, identityLabel); // 3. 构建Card所需的配置数据 // 注意:connectionProfile需要匹配你的Fabric网络实际配置 const cardData = { userName: identityIssueResponse.userID, businessNetwork: 'your-business-network', identity: { certificate: identityIssueResponse.certificate, privateKey: identityIssueResponse.privateKey }, connectionProfile: { name: 'hlfv1', type: 'hlfv1', description: 'Hyperledger Fabric v1.0', profile: { 'x-type': 'hlfv1', 'x-commitTimeout': 300, peers: [/* 你的peer节点配置 */], certificateAuthorities: [/* 你的CA配置 */], orderers: [/* 你的orderer配置 */] } } }; // 4. 创建Card实例并存储到文件系统 const newCard = new Card(cardData); const cardStore = new FileSystemCardStore(); await cardStore.put(newCard); console.log(`新身份Card已成功创建并存储,用户ID:${identityIssueResponse.userID}`); } catch (error) { console.error('创建Card失败:', error); process.exit(1); } } // 执行函数 createAndSaveNewCard();
3. 额外检查点
- 确认实际安装的版本:执行
npm list composer-common,检查输出的版本是否确实是0.19.0,避免package.json的版本约束导致安装了其他版本。 - 验证文件存在性:查看
node_modules/composer-common/lib/cardstore目录下是否有FileSystemCardStore.js文件,如果缺失,建议删除node_modules和package-lock.json后重新执行npm install。
内容的提问来源于stack exchange,提问作者K Cloud
相关产品推荐
相关产品推荐

