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,370 points
807 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
• 26,740 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 27, 2018 in Blockchain by Shashank
• 10,400 points
1,698 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,100 points
539 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 14, 2018 in Blockchain by Shashank
• 10,400 points
862 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,469 views
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
• 26,740 points
1,037 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
1,946 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
• 26,740 points
820 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
• 26,740 points
842 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP