1

Hi I am new in hyperledger fabric. Like every one I set up the fabric chain through documentation and got it working on my ubuntu with fabric samples.I need to create chain code in which 2 objects are needed and there relationship should be defined. In fabcar the application end is explained really well but I'm having issues incorporating 2 objects as only one object (car) is used in fabcar

Now I have created the both object in my chain code like with different functions

async createItem(ctx,id ,Name, status, description, owner) {
    

    const item = {
        Name,
        status,
        description,
        owner,
    };

    await ctx.stub.putState(id, Buffer.from(JSON.stringify(item)));
   
}

Now the issue i'm having is when I have to query them as described in fabcar function

async queryAllCars(ctx) {
    const startKey = '';
    const endKey = '';
    const allResults = [];
    for await (const {key, value} of ctx.stub.getStateByRange(startKey, endKey)) {
        const strValue = Buffer.from(value).toString('utf8');
        let record;
        try {
            record = JSON.parse(strValue);
        } catch (err) {
            console.log(err);
            record = strValue;
        }
        allResults.push({ Key: key, Record: record });
    }
    console.info(allResults);
    return JSON.stringify(allResults);
}

Here you can see threes no car identifier where Its saying specifically to the chain code that all entries of cars are needed .. So I was wondering If we have 2 objects in chain code how can it be done so I can tell the chain code specifically to get me Items or customers. I'm sorry for asking such a basic questions but please help me.

  • I don't have knowledge on javascript, but you can follow the given link where the code is written on go and that has 3 object. Each structure on that code works as an object. https://github.com/Archstrategy/crossBorderFundsTransfer/blob/master/chaincode/banks/banks.go – Ta-seen Junaid Jul 20 '20 at 01:57
  • thanks I will take a look at this in detail .. – Syed Zain Hasan Jul 20 '20 at 07:06

1 Answers1

0

In chaincode you can easily do this. What I did was Add start key and end key like

CUST0 (startkey) CUST99999999999 (endkey)

At the time of customer Regeneration you must add Keys like the ones and keep in check of the order. Same if you are Registering Items you can add ITEM0 - ITEM999999999

this number 9999999999 is just so that all eateries of a specific objects are fetched