Hyperledger Fabric: How to Parse arrays in req.body in node js express?

0 votes

I am getting an array in request body like this:

[

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

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

I want to loop through this array and call the chaincode and then send the response. I have tried this:

     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 because I am passing two values in input, the 2nd value result overrides the first one. Can someone tell me how to pass both the parameters?

Nov 16, 2018 in Blockchain by digger
• 27,620 points
491 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

You can get the value from the key:

for (var key in req.body) {
  if (req.body.hasOwnProperty(key)) {
    let value = req.body[key];
    console.log( `value for ${key} is ${value}` )
  }
}
answered Nov 16, 2018 by Kavya

Related Questions In Blockchain

0 votes
1 answer

Docker: How to connect Node.js in container to Hyperledger Fabric?

You need to map ~/.composer/cards of your Docker host ...READ MORE

answered Oct 31, 2018 in Blockchain by Omkar
• 66,880 points
63 views
0 votes
1 answer
+1 vote
2 answers

How to return value from a chaincode in Hyperledger Fabric?

Hyperledger Fabric supports only 2 types of ...READ MORE

answered Jun 13, 2018 in Blockchain by Perry
• 16,450 points
635 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,620 points
51 views
+1 vote
1 answer
+2 votes
3 answers
0 votes
1 answer

Hyperledger Fabric: How to access transaction ID in invoke function?

You can access transaction ID in Invoke ...READ MORE

answered Nov 5, 2018 in Blockchain by Omkar
• 66,880 points
245 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.