【问题标题】:Hyperledger fabric composer transaction not workingHyperledger Fabric Composer 事务不起作用
【发布时间】:2019-02-27 14:37:13
【问题描述】:

我正在尝试添加这个名为 placeOrder 的事务我想添加一个 Customer 参与者 在创建Order 资产之前,并在处理此交易时映射其与Order 资产的关系。但是我得到了客户未定义的错误。有人可以帮忙吗?谢谢。

我的模型

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
}

script.js

/**
 * @param {org.isn.shop.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){ 
        const 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{
        const customer = await customerRegistry.get(orderRequest.customer.email);    
    }

    const order = await factory.newResource("org.isn.shop","Order",orderRequest.orderId);
    order.customer = customer.getIdentifier();
    order.item = orderRequest.item;
    const orderRegistry = await getAssetRegistry("org.isn.shop.Order");
    await orderRegistry.add(order);

    const PlaceOrderEvent = factory.newEvent("org.isn.shop","PlaceOrderEvent");
    placeOrderEvent.order = order;
    emit(placeOrderEvent);
}

【问题讨论】:

    标签: hyperledger-fabric hyperledger hyperledger-composer


    【解决方案1】:

    以下版本的模型和 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 文件)

    【讨论】:

      【解决方案2】:

      o Customer customer 需要变为 --> Customer customer 然后你将能够引用 orderRequest.customer.firstName 等也不确定你是否需要 order.customer = customer.getIdentifier(); 可能需要变为 order.customer.email = customer.getIdentifier();

      【讨论】:

      • 如果我删除了 customerExists 条件检查,代码就可以工作。请找到 pastebin pastebin.com/xhdqG8TG 。 (其他部分也有评论)
      猜你喜欢
      • 1970-01-01
      • 2017-12-24
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多