t: 实例 org.acme.seller.Car#HW7722 缺少必填字段 oldOwner
t: Instance org.acme.seller.Car#HW7722 missing required field oldOwner
我是区块链的新手,我正在尝试使用虚拟框在 Ubuntu 上安装的 hyperledger-composer 设置上的一些代码,但无法解决此错误。
我在提交交易时收到此错误:
t: 实例 org.acme.seller.Car#HW7722 缺少必填字段 oldOwner
下面是我尝试的代码:
**Sample.cto**
/**
* Sample business network definition.
*/
namespace org.acme.seller
asset Car identified by carNumber{
o String carNumber
o String carName
--> Owner oldOwner
}
participant Owner identified by ownerId{
o String ownerId
o String fname
o String lname
}
transaction Transfer{
--> Car car
--> Owner newOwner
}
----------------------
**Sample.js**
/**
* Sample transaction processor function.
* @param {org.acme.seller.Transfer} tx The sample transaction instance.
* @transaction
*/
function Transfer(tx) {
// Save the old value of the asset.
tx.car.oldOwner = tx.car.newOwner;
// Update the asset with the new value.
// tx.car1.value = tx.newValue;
// Get the asset registry for the asset.
return getAssetRegistry('org.acme.seller.Car')
.then(function (assetRegistry) {
// Update the asset in the asset registry.
return assetRegistry.update(tx.car);
});
}
------------------------------------------------
**Permissions.acl**
/**
* Sample access control list.
*/
rule EverybodyCanReadEverything {
description: "Allow all participants read access to all resources"
participant: "ANY"
operation: ALL
resource: "org.acme.seller.*"
action: ALLOW
}
rule EverybodyCanSubmitTransactions {
description: "Allow all participants to submit transactions"
participant: "ANY"
operation: ALL
resource: "org.acme.seller.*"
action: ALLOW
}
rule OwnerHasFullAccessToTheirAssets {
description: "Allow all participants full access to their assets"
participant(p): "org.acme.seller.*"
operation: ALL
resource(r): "org.acme.seller.*"
condition: (r.owner.getIdentifier() === p.getIdentifier())
action: ALLOW
}
rule SystemACL {
description: "System ACL to permit all access"
participant: "org.hyperledger.composer.system.Participant"
operation: ALL
resource: "org.hyperledger.composer.system.**"
action: ALLOW
}
如果您向资产添加了新字段并且资产注册表中的现有数据没有该字段,您会看到此错误。
如果您在新字段中添加可选内容,例如--> Owner oldOwner optional
该错误应该会消失。
有一个Bootstrap函数总是好的,它可以帮助您加载少量资产和参与者来测试您的函数。在您的情况下,交易需要您之前应该创建的 owner participant。这有助于将汽车从一个所有者转移到另一个所有者。
考虑到这种情况,请执行以下操作,而不是像@r-thatcher 提到的那样将其设为可选。 创建两个所有者然后尝试在交易时通过提供所有者id来进行交易。这将帮助您清楚地理解它。
我是区块链的新手,我正在尝试使用虚拟框在 Ubuntu 上安装的 hyperledger-composer 设置上的一些代码,但无法解决此错误。
我在提交交易时收到此错误:
t: 实例 org.acme.seller.Car#HW7722 缺少必填字段 oldOwner
下面是我尝试的代码:
**Sample.cto**
/**
* Sample business network definition.
*/
namespace org.acme.seller
asset Car identified by carNumber{
o String carNumber
o String carName
--> Owner oldOwner
}
participant Owner identified by ownerId{
o String ownerId
o String fname
o String lname
}
transaction Transfer{
--> Car car
--> Owner newOwner
}
----------------------
**Sample.js**
/**
* Sample transaction processor function.
* @param {org.acme.seller.Transfer} tx The sample transaction instance.
* @transaction
*/
function Transfer(tx) {
// Save the old value of the asset.
tx.car.oldOwner = tx.car.newOwner;
// Update the asset with the new value.
// tx.car1.value = tx.newValue;
// Get the asset registry for the asset.
return getAssetRegistry('org.acme.seller.Car')
.then(function (assetRegistry) {
// Update the asset in the asset registry.
return assetRegistry.update(tx.car);
});
}
------------------------------------------------
**Permissions.acl**
/**
* Sample access control list.
*/
rule EverybodyCanReadEverything {
description: "Allow all participants read access to all resources"
participant: "ANY"
operation: ALL
resource: "org.acme.seller.*"
action: ALLOW
}
rule EverybodyCanSubmitTransactions {
description: "Allow all participants to submit transactions"
participant: "ANY"
operation: ALL
resource: "org.acme.seller.*"
action: ALLOW
}
rule OwnerHasFullAccessToTheirAssets {
description: "Allow all participants full access to their assets"
participant(p): "org.acme.seller.*"
operation: ALL
resource(r): "org.acme.seller.*"
condition: (r.owner.getIdentifier() === p.getIdentifier())
action: ALLOW
}
rule SystemACL {
description: "System ACL to permit all access"
participant: "org.hyperledger.composer.system.Participant"
operation: ALL
resource: "org.hyperledger.composer.system.**"
action: ALLOW
}
如果您向资产添加了新字段并且资产注册表中的现有数据没有该字段,您会看到此错误。
如果您在新字段中添加可选内容,例如--> Owner oldOwner optional
该错误应该会消失。
有一个Bootstrap函数总是好的,它可以帮助您加载少量资产和参与者来测试您的函数。在您的情况下,交易需要您之前应该创建的 owner participant。这有助于将汽车从一个所有者转移到另一个所有者。
考虑到这种情况,请执行以下操作,而不是像@r-thatcher 提到的那样将其设为可选。 创建两个所有者然后尝试在交易时通过提供所有者id来进行交易。这将帮助您清楚地理解它。