我正在尝试添加名为[
{
"id": "1",
"timestamp": "valid-timestamp1",
"value-1": "1",
"value-2": "2",
"value-3": "3"
},
{
"id": "1",
"timestamp": "valid-timestamp1",
"value-1": "1",
"value-2": "2",
"value-3": "3"
}
]
的交易,我想添加一个placeOrder
参与者
在创建Customer
资产并在处理此交易时映射其与Order
资产的关系。但是我收到客户未定义的错误。有人可以帮忙吗?谢谢。
我的模特
Order
script.js
namespace org.isn.customer
participant Customer identified by email {
o String firstName
o String lastName
o String email
o String password
}
enum Status{
o ACTIVE
o OFF_THE_ROAD
}
asset Vehicle identified by serial {
o String brand
o String model
o String color
o Status status
o Double price
o String serial
}
asset Order identified by orderId{
o String orderId
o Vehicle item
--> Customer customer
}
transaction PlaceOrder {
o String orderId
--> Vehicle item
o Customer customer
}
答案 0 :(得分:0)
o Customer customer
需要成为--> Customer customer
,然后您才能引用orderRequest.customer.firstName
等,也不确定是否需要order.customer = customer.getIdentifier();
可能需要成为order.customer.email = customer.getIdentifier();
>
答案 1 :(得分:0)
以下版本的模型和JS将适用于新客户和现有客户:
namespace org.isn.customer
participant Customer identified by email {
o String firstName
o String lastName
o String email
o String password
}
enum Status{
o ACTIVE
o OFF_THE_ROAD
}
asset Vehicle identified by serial {
o String brand
o String model
o String color
o Status status
o Double price
o String serial
}
asset Order identified by orderId{
o String orderId
--> Vehicle item
--> Customer customer
}
transaction PlaceOrder {
o String orderId
--> Vehicle item
o Customer customer
}
/**
* @param {org.isn.customer.PlaceOrder}orderRequest
* @transaction
*/
async function placeOrder(orderRequest){
const factory = getFactory();
const customerRegistry = await getParticipantRegistry("org.isn.customer.Customer");
const customerExists = await customerRegistry.exists(orderRequest.customer.email);
if(!customerExists){
var customer = factory.newResource("org.isn.customer","Customer",orderRequest.customer.email);
customer.firstName = orderRequest.customer.firstName;
customer.lastName = orderRequest.customer.lastName;
customer.email = orderRequest.customer.email;
customer.password = orderRequest.customer.password;
await customerRegistry.add(customer);
}else{
var customer = await customerRegistry.get(orderRequest.customer.email);
}
const order = await factory.newResource("org.isn.customer","Order",orderRequest.orderId);
order.customer = factory.newRelationship("org.isn.customer","Customer",customer.getIdentifier());
order.item = orderRequest.item;
const orderRegistry = await getAssetRegistry("org.isn.customer.Order");
await orderRegistry.add(order);
}
请注意以下几点:
在订单资产中,我将o Vehicle
更改为--> Vehicle item
以与交易匹配。
我还在if和else代码中都使用了var customer
。
(要使我的工作正常,我必须使用相同的名称空间,但也许您有单独的.cto文件)