1

I am getting error when adding new data in hyperledger.

Update working fine.

Here is my some code where i am facing issue

if(isExist) {
                const oldProOwnVal =  await PropertyOwnersRegistry.get(isExist.ownershipId);

                owners.ownership_start_date = oldProOwnVal.ownership_start_date;
                owners.created_at = oldProOwnVal.created_at;
                owners.updated_at = updatedProperty.timestamp;
                const mergeOwner = Object.assign(oldProOwnVal, owners);
                await PropertyOwnersRegistry.update(mergeOwner);
            } else {
                newKey =  'xxxx-xxxx-4xxx-yxxx-xxxx'.replace(/[xy]/g, function(c) {
                    var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
                    return v.toString(16);
                });
                const newOwnerRes = factory.newResource(NS, 'PropertyOwners', newKey);


                owners.ownership_start_date = updatedProperty.timestamp;
                owners.created_at = updatedProperty.timestamp;
                owners.updated_at = updatedProperty.timestamp;
                const newOwner = Object.assign(newOwnerRes, owners);
                await PropertyOwnersRegistry.add(newOwner);
            }

Issue only occur in line

await PropertyOwnersRegistry.add(newOwner);

Don't know what happen here.

Tariq
  • 188
  • 1
  • 15

2 Answers2

1

it might be how you're setting up, prior to calling the Composer APIs - eg getAssetRegistry (assets) or getParticipantRegistry(participants) - or else its out of scope for the add in part 2. I didn't see your model, or prior code - so supplementing as appropriate below:

this should work (not tried) - note - you have non-deterministic code, if you're testing with endorsement etc:

const NS = 'org.acme.example';
var factory = getFactory();

const  propRegistry = await  getAssetRegistry(NS+ '.PropertyOwners'');

if(isExist) {
    const oldProOwnVal =  await propRegistry.get(isExist.ownershipId);

    owners.ownership_start_date = oldProOwnVal.ownership_start_date;
    owners.created_at = oldProOwnVal.created_at;
    const mergeOwner = Object.assign(oldProOwnVal, owners);
   //etc

    await propRegistry.update(mergeOwner);
 } else {           
     newKey =  'xxxx-xxxx-4xxx-yxxx-xxxx'.replace(/[xy]/g, function(c) {
                    var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);  // this code is non-deterministic
                    return v.toString(16);
                });
     const newOwnerRes = factory.newResource(NS, 'PropertyOwners', newKey);
     owners.ownership_start_date = updatedProperty.timestamp;
     owners.created_at = updatedProperty.timestamp;
     owners.updated_at = updatedProperty.timestamp;

     const newOwner = Object.assign(newOwnerRes, owners);

     await propRegistry.add(newOwner);
 }
Paul O'Mahony
  • 6,740
  • 1
  • 10
  • 15
  • I haven't add full code previously. actualy i already have propRegistry defined in the top. Wehn i remove await propRegistry.add(newOwner); line, the error will gone. – Tariq Oct 26 '18 at 10:14
  • Can i share a BNA file ? – Tariq Oct 26 '18 at 10:25
  • right, The heading suggests your ACLs are possibly the 'issue'. Have you checked, whether the code works - without constraining ACLs ? - that would be the next logical step (to eliminate a code issue) - BNAs via rocketchat (as .zip not .bna) – Paul O'Mahony Oct 26 '18 at 10:42
  • 1
    ok, had a look at your code. The problem is in 2 places 1) you're using forEach with an async loop - see https://stackoverflow.com/questions/45914162/javascript-async-await-doesnt-work-inside-foreach-loop - in any case you're providing the list from your txn object so use `for (let owners of propertyOwnersList) ` instead to process `PropertyOwners` assets (they should be participants really) and the await takes care of the add and ... – Paul O'Mahony Oct 26 '18 at 14:20
  • 1
    2) you don't provide required values for the new PropertyOwner asset instance (such as (eg) newOwnerRes.is_owner = false; newOwnerRes.trustee_ownership_flag = "YES";` Also you need to build a newRelationship for `property` field eg `owners.property = factory.newRelationship(NS, 'Property', propertId);` Suffice to say, it works for me - can add Property and 2 x PropertyOwners from your sample JSON – Paul O'Mahony Oct 26 '18 at 14:20
1

I had a similar problem, but for my case I was updating two assets at ago and I was not waiting for the other to finish. This what I mean

  const prodRegistry = await getAssetRegistry('org.trade.com.Product');
  const chequeReg = await getAssetRegistry('org.trade.com.Cheque');
  chequeRegistry.update(cheque);
  await prodRegistry.updateAll(products);

The fix is to wait for the first registry to finish updating the asset before updating the other.

await chequeReg.update(cheque);
await prodRegistry.updateAll(products);
Ivan
  • 759
  • 1
  • 10
  • 19