hyperledger / iroha-javascript

JavaScript library for Iroha, a Distributed Ledger Technology (blockchain) platform.
https://wiki.hyperledger.org/display/iroha
Apache License 2.0
94 stars 64 forks source link

error executing FindTransactionsByAccountId query #184

Closed matisalimbene closed 5 months ago

matisalimbene commented 6 months ago

I'm unable to execute the FindTransactionsByAccountId query. I wasn't able to find any similar example for follow on how to construct the query. (Tests use the sugar library).

const getTxLog = async (accountId) => {

  const {
    AccountId,
    QueryBox,
    Expression,
    Value,
    IdBox,
  } = datamodel;
  const [accName, accDomainId] = accountId.split('@');
  const accDefId = sugar.accountId(accName, accDomainId);

  const account = AccountId({
    name: accName,
    domain_id: accDomainId,
  });

  cons query = QueryBox('FindTransactionsByAccountId', {
    account_id: Expression('Raw', Value('Id', IdBox('AccountId', account))),
  });

  const { pre, client } = _clientFactory();
  const result = await client.requestWithQueryBox(pre, query);
  return { data: result };
};

Right now this is the err I'm getting:

Cannot read properties of undefined (reading 'length')

These are the documentation Links I followed:

0x009922 commented 5 months ago

The issue is that you are constructing the account id incorrectly:

const getTxLog = async (accountId) => {  
  // ...

  const [accName, accDomainId] = accountId.split('@');  

  const account = AccountId({  
    name: accName,  
    domain_id: accDomainId,
    //         ^^^^^^^^^^^ - got `string`, expected `DomainId`
  });

  // ...
}

When codec tries to encode the domain_id field, it passes the domain_id.name field into a String codec, which looks up for a .length field, and that's where you get the

Cannot read properties of undefined (reading 'length')

error.

So, the fix would be to construct a proper DomainId:

const account = AccountId({  
  name: accName,  
  domain_id: datamodel.DomainId({ name: accDomainId })  
})

Once again, I strongly recommend you using TypeScript, because this error can be easily caught using its diagnostics. For example, if you annotate the accountId argument with string type, TypeScript highlights the error:

const getTxLog = async (accountId: string) => {  
  //                             ^^^^^^^^ add this

  // ...

  const account = AccountId({  
    name: accName,  
    domain_id: accDomainId,
//  ^^^^^^^^^ TS2322: Type string is not assignable to type DomainId
  });

  // ...
}