Javascript Hyperledger编写器检查数组

Javascript Hyperledger编写器检查数组,javascript,hyperledger,hyperledger-composer,Javascript,Hyperledger,Hyperledger Composer,我在model.cto文件中定义了一个数组Account[]family,我想从logic.js访问它。特别是,我只想在接收方位于发送方的家族数组中时执行事务 My model.cto: namespace org.digitalpayment asset Account identified by accountId { o String accountId --> Customer owner o Double balance } participant Custome

我在model.cto文件中定义了一个数组
Account[]family
,我想从logic.js访问它。特别是,我只想在接收方位于发送方的家族数组中时执行事务

My model.cto:

namespace org.digitalpayment

asset Account identified by accountId {
  o String accountId
  --> Customer owner
  o Double balance
}

participant Customer identified by customerId {
  o String customerId
  o String firstname
  o String lastname
  --> Account[] family optional
}

transaction AccountTransfer {
--> Account from
--> Account to
o Double amount
}
My logic.js:

/**
* Account transaction
* @param {org.digitalpayment.AccountTransfer} accountTransfer
* @transaction
*/
async function accountTransfer(accountTransfer) {
    if (accountTransfer.from.balance < accountTransfer.amount) {
        throw new Error("Insufficient funds");
    }

    if (/*TODO check if the family array contains the receiver account*/) {        

        // perform transaction
        accountTransfer.from.balance -= accountTransfer.amount;
        accountTransfer.to.balance += accountTransfer.amount;

        let assetRegistry = await getAssetRegistry('org.digitalpayment.Account');

        await assetRegistry.update(accountTransfer.from);
        await assetRegistry.update(accountTransfer.to);

    } else {
        throw new Error("Receiver is not part of the family");
    }

}
/**
*账户交易
*@param{org.digitalpayment.AccountTransfer}AccountTransfer
*@交易
*/
异步函数accountTransfer(accountTransfer){
if(accountTransfer.from.balance
好的,那么基本上您希望首先获取
系列
资产的所有帐户,然后检查
客户
参与者是否包括在其中?如果我错了,请纠正我。 一组合乎逻辑的步骤是-

  • 根据<代码>至<代码>和<代码>自<代码>输入检索<代码>帐户<代码>
  • 使用
    所有者
    变量为每个
    帐户
    检索每个
    客户
  • 从每个
    客户
  • /**
    *账户交易
    *@param{org.digitalpayment.AccountTransfer}AccountTransfer
    *@交易
    */
    异步函数accountTransfer(accountTransfer){
    if(accountTransfer.from.balance

    由于最近几次
    Composer中的语法更改,根据您在项目中使用的版本,版本可能无法工作。如果这不起作用,并且您使用的是旧版本,请告诉我,我将相应地更新答案。

    这起作用,但将
    从CustomerFamily.includes(to Customer)
    替换为
    从CustomerFamily.includes(to)
    ,因为数组族中填充的是帐户ID,而不是客户ID。此外,还需要检查数组是否已定义(因为它是可选的)。这可以通过
    if(typeof fromcustomefamily!==“undefined”&&fromcustomefamily.length>0){}
    完成,否则事务以“TypeError:Cannot read property”includes“of undefined”结束。非常感谢您的帮助。啊,对了,我还以为家庭阵列是客户呢。从逻辑上说,这让我很吃惊。您可以简单地执行
    if(来自CustomerFamily)
    ,而不是使用
    typeof
    。您不需要检查数组的长度是否为0,因为
    includes
    函数将在没有它的情况下运行。我根据你的评论编辑了这份声明
    /**
    * Account transaction
    * @param {org.digitalpayment.AccountTransfer} accountTransfer
    * @transaction
    */
    async function accountTransfer(accountTransfer) {
        if (accountTransfer.from.balance < accountTransfer.amount) {
            throw new Error("Insufficient funds");
        };
    
        var from = accountTransfer.from;
        var to = accountTransfer.to;
        var fromCustomer = from.owner;
        var toCustomer = to.owner;
        var fromCustomerFamily = fromCustomer.family;
    
        if (fromCustomerFamily && fromCustomerFamily.includes(to)) {        
    
            // perform transaction
            accountTransfer.from.balance -= accountTransfer.amount;
            accountTransfer.to.balance += accountTransfer.amount;
    
            let assetRegistry = await getAssetRegistry('org.digitalpayment.Account');
    
            await assetRegistry.update(accountTransfer.from);
            await assetRegistry.update(accountTransfer.to);
    
        } else {
            throw new Error("Receiver is not part of the family");
        }
    
    }