部署在 Google Compute Engine 上时的 Hyperledger Composer 模块 local_connection.json
Hyperledger Composer module local_connection.json when deployed on Google Compute Engine
我有一个 hyperledger composer 项目可以在本地主机上顺利运行,但我试图将它部署到 Google Cloud Compute Engine 并且无法正常工作。
该项目分为两部分:composer 网络和与网络交互的 nodejs 后端。 T
我跟随开发人员并从 Composer 站点部署到单一组织教程。
nodejs 服务器处理注册和登录逻辑并公开其余 api 以与客户端交互。这是我的代码:
index.js
//post call to register graduate on the network
app.post('/api/registerGraduate', function(req, res) {
console.log("Creando cuenta graduado");
var graduateRut = req.body.graduaterut;
var cardId = req.body.cardid;
var firstName = req.body.firstname;
var lastName = req.body.lastname;
var email = req.body.email;
var phoneNumber = req.body.phonenumber;
network.registerGraduate(cardId, graduateRut, firstName, lastName, email, phoneNumber)
.then((response) => {
//return error if error in response
if (response.error != null) {
res.json({
error: response.error
});
} else {
//else return success
res.json({
success: response
});
}
});
});
network.js
registerGraduate: async function (cardId, graduateRut,firstName, lastName, email, phoneNumber) {
try {
//connect as admin
businessNetworkConnection = new BusinessNetworkConnection();
await businessNetworkConnection.connect('admin@degree');
//get the factory for the business network
factory = businessNetworkConnection.getBusinessNetwork().getFactory();
//create graduate participant
const graduate = factory.newResource(namespace, 'Graduate', graduateRut);
graduate.firstName = firstName;
graduate.lastName = lastName;
graduate.email = email;
graduate.phoneNumber = phoneNumber;
//add graduate participant
const participantRegistry = await businessNetworkConnection.getParticipantRegistry(namespace + '.Graduate');
await participantRegistry.add(graduate);
//issue identity
const identity = await businessNetworkConnection.issueIdentity(namespace + '.Graduate#' + graduateRut, cardId);
//import card for identity
await importCardForIdentity(cardId, identity);
//disconnect
await businessNetworkConnection.disconnect('admin@degree');
return true;
}
catch(err) {
//print and return error
console.log(err);
var error = {};
error.error = err.message;
return error;
}
},
我正在与 postman 一起测试其余的 api。我返回的错误是 "error": "Cannot find module './local_connection.json'"
但这仅在我第一次发送 post 请求时发生。第二次报错是Error: Failed to add object with ID '166438715' in collection with ID 'Participant:org.degree.ucsd.University' as the object already exists
.
显然,尽管错误是将数据写入区块链,但如果我尝试获取数据,我会收到一条错误消息,指出 cardid
未找到。
那么,我错过了什么?如果它在 localhost 上工作但在 Google VM 上不工作,是身份验证吗?我该如何解决这个问题?
感谢您的帮助!
编辑!
importCardForIdentity 函数:
async function importCardForIdentity(cardName, identity) {
//use admin connection
adminConnection = new AdminConnection();
businessNetworkName = 'degree';
//declare metadata
const metadata = {
userName: identity.userID,
version: 1,
enrollmentSecret: identity.userSecret,
businessNetwork: businessNetworkName
};
//get connectionProfile from json, create Idcard
const connectionProfile = require('./local_connection.json');
const card = new IdCard(metadata, connectionProfile);
//import card
await adminConnection.importCard(cardName, card);
}
报告的第二个错误是因为(正如您所指出的)参与者已经创建。
真正的问题是试图从您的客户端 REST API 或邮递员中找到新发行的 'graduateRut' 业务网卡 ('not found') - 在您当前的设置中它会寻找这在 $HOME/.composer 下的同一客户端上 - 这是在与您的 nodeJS 后端不同的节点上吗?
您的 ImportCardForIdentity
函数是否也像这里显示的那样?
Unable to issue identity card using Hyperledger Composer Javascript API
我有一个 hyperledger composer 项目可以在本地主机上顺利运行,但我试图将它部署到 Google Cloud Compute Engine 并且无法正常工作。
该项目分为两部分:composer 网络和与网络交互的 nodejs 后端。 T
我跟随开发人员并从 Composer 站点部署到单一组织教程。
nodejs 服务器处理注册和登录逻辑并公开其余 api 以与客户端交互。这是我的代码:
index.js
//post call to register graduate on the network
app.post('/api/registerGraduate', function(req, res) {
console.log("Creando cuenta graduado");
var graduateRut = req.body.graduaterut;
var cardId = req.body.cardid;
var firstName = req.body.firstname;
var lastName = req.body.lastname;
var email = req.body.email;
var phoneNumber = req.body.phonenumber;
network.registerGraduate(cardId, graduateRut, firstName, lastName, email, phoneNumber)
.then((response) => {
//return error if error in response
if (response.error != null) {
res.json({
error: response.error
});
} else {
//else return success
res.json({
success: response
});
}
});
});
network.js
registerGraduate: async function (cardId, graduateRut,firstName, lastName, email, phoneNumber) {
try {
//connect as admin
businessNetworkConnection = new BusinessNetworkConnection();
await businessNetworkConnection.connect('admin@degree');
//get the factory for the business network
factory = businessNetworkConnection.getBusinessNetwork().getFactory();
//create graduate participant
const graduate = factory.newResource(namespace, 'Graduate', graduateRut);
graduate.firstName = firstName;
graduate.lastName = lastName;
graduate.email = email;
graduate.phoneNumber = phoneNumber;
//add graduate participant
const participantRegistry = await businessNetworkConnection.getParticipantRegistry(namespace + '.Graduate');
await participantRegistry.add(graduate);
//issue identity
const identity = await businessNetworkConnection.issueIdentity(namespace + '.Graduate#' + graduateRut, cardId);
//import card for identity
await importCardForIdentity(cardId, identity);
//disconnect
await businessNetworkConnection.disconnect('admin@degree');
return true;
}
catch(err) {
//print and return error
console.log(err);
var error = {};
error.error = err.message;
return error;
}
},
我正在与 postman 一起测试其余的 api。我返回的错误是 "error": "Cannot find module './local_connection.json'"
但这仅在我第一次发送 post 请求时发生。第二次报错是Error: Failed to add object with ID '166438715' in collection with ID 'Participant:org.degree.ucsd.University' as the object already exists
.
显然,尽管错误是将数据写入区块链,但如果我尝试获取数据,我会收到一条错误消息,指出 cardid
未找到。
那么,我错过了什么?如果它在 localhost 上工作但在 Google VM 上不工作,是身份验证吗?我该如何解决这个问题?
感谢您的帮助!
编辑!
importCardForIdentity 函数:
async function importCardForIdentity(cardName, identity) {
//use admin connection
adminConnection = new AdminConnection();
businessNetworkName = 'degree';
//declare metadata
const metadata = {
userName: identity.userID,
version: 1,
enrollmentSecret: identity.userSecret,
businessNetwork: businessNetworkName
};
//get connectionProfile from json, create Idcard
const connectionProfile = require('./local_connection.json');
const card = new IdCard(metadata, connectionProfile);
//import card
await adminConnection.importCard(cardName, card);
}
报告的第二个错误是因为(正如您所指出的)参与者已经创建。
真正的问题是试图从您的客户端 REST API 或邮递员中找到新发行的 'graduateRut' 业务网卡 ('not found') - 在您当前的设置中它会寻找这在 $HOME/.composer 下的同一客户端上 - 这是在与您的 nodeJS 后端不同的节点上吗?
您的 ImportCardForIdentity
函数是否也像这里显示的那样?
Unable to issue identity card using Hyperledger Composer Javascript API