我正在尝试通过我的角度应用程序创建网络的新用户。 为此:
角度代码:
return this.httpClient.post('http://<IP>:3001/participant", participant, {headers: this.httpHeadersAuth}).toPromise()
节点服务器代码:
app.use(bodyParser.json());
app.post("/participant", function(req, res) {
const ParticipantAdd = require('composer-cli').Participant.Add;
console.log(req.body)
let newParticipant = '{ ' +
'"$class": "vaccinspace.Parent",' +
' "idParent" : "' + req.body.idParent + '",' +
' "name" : "' + req.body.name + '",' +
' "surname" : "' + req.body.surname + '",' +
' "phone" : "' + req.body.phone + '",' +
' "email" : "' + req.body.email +
' "}';
console.log(newParticipant)
let options = {
card: 'admin@vaccin-network',
data: newParticipant
};
ParticipantAdd.handler(options)
.then((response) => {
res.json({
message: response,
idUser: newParticipant.idParent
});
})
.catch((error) => {
res.status(401).json({
message: error
});
});
});
角度代码:
const identity = '{ ' +
'"participant": "resource:vaccinspace.Parent#' + user['id'] + '",' +
'"userID" : "' + user['id'] +
'"}';
return this.httpClient.post(this.URLAuth + "identity", identity, {responseType: 'blob',headers: this.httpHeadersAuth}).toPromise()
节点服务器代码:
app.post("/identity", function(req, res) {
const IdCard = require('composer-common').IdCard;
const BusinessNetworkConnection = require('composer-client').BusinessNetworkConnection;
const AdminConnection = require('composer-admin').AdminConnection;
const CardExport = require('composer-cli').Card.Export;
const ParticipantAdd = require('composer-cli').Participant.Add;
let businessNetworkConnection = new BusinessNetworkConnection();
const adminConnection = new AdminConnection();
var cardName;
const participant = req.body.participant;
const userID = req.body.userID;
return businessNetworkConnection.connect('admin@vaccin-network')
.then(() => {
// Create a new identity for user
return businessNetworkConnection.issueIdentity(participant, userID)
})
.then((identity) => {
// Create and import card
const metadata = {
userName: identity.userID,
version: 1,
enrollmentSecret: identity.userSecret,
role: "parent",
businessNetwork: "vaccin-network"
};
cardName = metadata.role + metadata.userName + "@" + metadata.businessNetwork
const connectionProfile = {...}
const card = new IdCard(metadata, connectionProfile);
return adminConnection.importCard(cardName, card)
})
.then(() => {
return adminConnection.exportCard(cardName);
})
.then((response) => {
res.type('binary');
res.send(response)
})
.then(() => {
//Se déconnecter
businessNetworkConnection.disconnect()
})
.catch((error) => {
res.status(401).json({
message: error
});
});
});
/api/wallet/import?=<name>
角度代码:(通过创建身份返回“卡数据”)
const file = new File([cardData], user['id'] + '.card', {
type: 'application/octet-stream',
lastModified: Date.now()
});
const formData = new FormData();
formData.append('card', file);
const headers = new HttpHeaders();
headers.set('Authorization', this.CookieService.get('access_token').split(':')[1].split('.')[0]);
return this.httpClient.post('https://<IP>:3000/api/wallet/import?name=' + user['id'], formData, {withCredentials: true,headers}).toPromise();
})
但是此帖子不起作用,我的其余服务器日志上有此错误:
请求POST / api / wallet / import?name = 9d ...的未处理错误:错误: 找不到中央目录的末尾:这是一个zip文件吗?如果是, 参见http://stuk.github.io/jszip/documentation/howto/read_zip.html
有人知道如何解决这个问题? 有关信息:卡已创建(我可以在“ composer卡列表”中看到它) 我导出了这张卡(带有“ composer card export ....”。),我贴了这张卡: -在作曲家其余服务器api上 -与邮递员(与我的有角度的邮递员相同的参数) 而且效果很好!
感谢您的帮助!
答案 0 :(得分:0)
您需要将内容类型以及multipart / formdata添加到标头请求中。
祝你好运。
答案 1 :(得分:0)
我遇到了类似的问题,这是因为我的环回钱包数据库(MySQL)中Card表的base64列太小了。自动更新已将该字段转换为VARCHAR(512),并且数据已被截断。我将该列增加到VARCHAR(2048)并解决了问题。