How to handle Overlapping of array objects in node js

0 votes

I am getting an array in request body like :

[
   {
     "month": "JUL",
     "year": "2018"
   },
   {
     "month": "JAN",
     "year": "2018"
   },
   {
     "month": "MAR",
     "year": "2018"
   }
 ]

This input has two parameters (month:enum and year:string).

I need to loop through this array and call the chaincode and finally send the response . I have done the following :

for (var i = 0; i < req.body.length; i++) {
  var month = req.body[i].month;
  var year = req.body[i].year;
  var monthYear = month + year;
  key = monthYear + "_new";
  console.log("Key is ", key);
  var request = {
    //targets: let default to the peer assigned to the client
    chaincodeId: 'abc',
    fcn: 'getTransactionsByKey',
    args: [key]

    //Calling chaincode smartcontract
    return channel.queryByChaincode(request);
  }

but the response is coming correct if I pass only one input parameter . If I pass two values in input , the 2nd value result overrides the first one. Can someone please tell me how to handle this?

Sep 18, 2018 in Blockchain by slayer
• 29,050 points
31 views

1 answer to this question.

0 votes

Try this code, may be it works for you

var request=[];
 for(var i=0; i<req.body.length; i++) {
                    var month =req.body[i].month;
                    var year = req.body[i].year;
                    var monthYear = month + year;
    key = monthYear + "_new";
        console.log("Key is ", key);
             request.push( {
            //targets: let default to the peer assigned to the client
             chaincodeId: 'abc',
             fcn: 'getTransactionsByKey',
             args: key
             });

//Calling chaincode smartcontract
}
  console.log(request);
 return channel.queryByChaincode(request);

Output:

[ { chaincodeId: 'abc',
    fcn: 'getTransactionsByKey',
    args: 'JAN2018_new' },
  { chaincodeId: 'abc',
    fcn: 'getTransactionsByKey',
    args: 'JUL2018_new' },
  { chaincodeId: 'abc',
    fcn: 'getTransactionsByKey',
    args: 'MAR2018_new' } ]
answered Sep 18, 2018 by digger
• 27,630 points

Related Questions In Blockchain

0 votes
1 answer

How to create tokens in smart contracts from node.js?

Any modification to the Ethereum Blockchain will ...READ MORE

answered Jun 26, 2018 in Blockchain by Shashank
• 10,330 points
132 views
0 votes
1 answer

In a non-cryptocurrency implementation of Blockchain, how to successfully mine a node?

Difficulty is a property of the blockchain, ...READ MORE

answered Jul 13, 2018 in Blockchain by Perry
• 17,010 points
37 views
0 votes
1 answer

How to handle memory and processor limitations of nodes in blockchain?

Yes, that's a valid concern. The Bitcoin ...READ MORE

answered Jul 13, 2018 in Blockchain by Shashank
• 10,330 points
23 views
0 votes
1 answer
0 votes
1 answer

Invalid Batch or signature in Savtooth

This will solve your problem import org.apache.commons.codec.binary.Hex; Transaction txn ...READ MORE

answered Aug 1, 2018 in Blockchain by digger
• 27,630 points
58 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
126 views
0 votes
1 answer

How to control visibility of assets in Hyperledger Fabric?

you can use proof of Authority consensus ...READ MORE

answered Jul 10, 2018 in Blockchain by digger
• 27,630 points
96 views
0 votes
1 answer

How to add https security for Node js?

You need to add the key and cert to the createServer function. const options ...READ MORE

answered Aug 10, 2018 in Blockchain by digger
• 27,630 points
35 views