Hyperledger Composer 通过 Javascript API 添加参与者

Hyperledger Composer Add Participant through Javascript API

我可以通过如下命令行命令成功添加参与者。

composer participant add -c admin@health-network -d '{"$class": "org.acme.health.Patient", "patientId": "10", "firstName": "Mae", "lastName": "smith", "email": "smith@gmail.com"}'

但是,当我尝试 javascript API 时,出现了这个错误。

错误:尝试查询业务网络时出错。错误:链代码错误(状态:500,消息:错误:ID 为“$sysregistries”的集合中 ID 为 'Participant:org.acme.health' 的对象不存在) 在 channel.queryByChaincode.then.catch (/home/sara/health-network/angular-app/node_modules/composer-connector-hlfv1/lib/hlfconnection.js:809:34) 在

这是我的 javascript 代码

const BusinessNetworkConnection = require('composer-client').BusinessNetworkConnection;
let businessNetworkConnection = new BusinessNetworkConnection();
return businessNetworkConnection.connect('admin@health-network')
.then(() => {
    return businessNetworkConnection.getParticipantRegistry('org.acme.health');
})

.then((participantRegistry) => {
    let factory = businessNetworkConnection.getFactory();
    let participant = factory.newResource('org.acme.health', 'Patient', '10');
    participant.firstName = 'Mae';
    participant.lastName = 'Smith';
    participant.email ='smith@gmail.com'
    return participantRegistry.add(participant);
})
.then(() => {
    return businessNetworkConnection.disconnect();
})
.catch((error) => {
    console.error(error);
    process.exit(1);

});

我的 .cto 文件中有一个名为 'Patient' 的参与者。

我发现了我需要在 getParticipantRegistery('org.acme.health.Patient') 中定义资源的问题。

@ethertest,我已将您的代码格式化为 below.Hope 它有效。

const BusinessNetworkConnection = require('composer-client').BusinessNetworkConnection;
let businessNetworkConnection = new BusinessNetworkConnection();

return businessNetworkConnection.connect('admin@health-network')
.then((bconnect) => {
    let bfactory = bconnect.getFactory();

    return Promise.all([businessNetworkConnection.getParticipantRegistry('org.acme.health.Patient'), bfactory]);
})
.then((participantRegistry1) => {
    participantRegistry = participantRegistry1[0];
    bfactory = participantRegistry1[1];

    let participant = bfactory.newResource('org.acme.biznet', 'Patient', 'patient1');
    participant.firstName = 'Bob';
    participant.lastName = 'Miller';
    return participantRegistry.add(participant);
})
.then(() => {
    return businessNetworkConnection.disconnect();
})
.catch((error) => {
    console.error(error);
    process.exit(1);
});