在hyperledger中添加新数据时出错。
更新工作正常。
这是我面临问题的一些代码
if(isExist) {
const oldProOwnVal = await PropertyOwnersRegistry.get(isExist.ownershipId);
owners.ownership_start_date = oldProOwnVal.ownership_start_date;
owners.created_at = oldProOwnVal.created_at;
owners.updated_at = updatedProperty.timestamp;
const mergeOwner = Object.assign(oldProOwnVal, owners);
await PropertyOwnersRegistry.update(mergeOwner);
} else {
newKey = 'xxxx-xxxx-4xxx-yxxx-xxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
const newOwnerRes = factory.newResource(NS, 'PropertyOwners', newKey);
owners.ownership_start_date = updatedProperty.timestamp;
owners.created_at = updatedProperty.timestamp;
owners.updated_at = updatedProperty.timestamp;
const newOwner = Object.assign(newOwnerRes, owners);
await PropertyOwnersRegistry.add(newOwner);
}
问题只发生在一行
await PropertyOwnersRegistry.add(newOwner);
不知道这里发生了什么。
它可能是你在调用Composer API之前的设置 - 例如getAssetRegistry
(assets)或getParticipantRegistry
(参与者) - 或者它在第2部分中超出了add
的范围。我没有看到你的模型,或者先前的代码 - 以下适当的补充:
这应该工作(没有尝试过) - 注意 - 你有非确定性代码,如果你正在测试代言等:
const NS = 'org.acme.example';
var factory = getFactory();
const propRegistry = await getAssetRegistry(NS+ '.PropertyOwners'');
if(isExist) {
const oldProOwnVal = await propRegistry.get(isExist.ownershipId);
owners.ownership_start_date = oldProOwnVal.ownership_start_date;
owners.created_at = oldProOwnVal.created_at;
const mergeOwner = Object.assign(oldProOwnVal, owners);
//etc
await propRegistry.update(mergeOwner);
} else {
newKey = 'xxxx-xxxx-4xxx-yxxx-xxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); // this code is non-deterministic
return v.toString(16);
});
const newOwnerRes = factory.newResource(NS, 'PropertyOwners', newKey);
owners.ownership_start_date = updatedProperty.timestamp;
owners.created_at = updatedProperty.timestamp;
owners.updated_at = updatedProperty.timestamp;
const newOwner = Object.assign(newOwnerRes, owners);
await propRegistry.add(newOwner);
}
我遇到了类似的问题,但就我的情况而言,我之前更新了两个资产,而我并没有等到另一个完成。这就是我的意思
const prodRegistry = await getAssetRegistry('org.trade.com.Product');
const chequeReg = await getAssetRegistry('org.trade.com.Cheque');
chequeRegistry.update(cheque);
await prodRegistry.updateAll(products);
修复是等待第一个注册表在更新另一个之前完成资产更新。
await chequeReg.update(cheque);
await prodRegistry.updateAll(products);