javascript - Parsing arrays in req.body in node js express - Stack Overflow

I am getting an array in request body like :[{"month" : "JUL","year" :&qu

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 ing correct if I pass only one input parameter . If I pass two values in input , the 2nd value result overrides the first one . Any help on how can I get response for all the list of inputs with the overlapping part.

Also , I need to sort the input values before calling the chaincode , like if I get Feb Mar Jan in the input , I should sort it as Jan Feb Mar and then run the for loop.

Any help on this is appreciated.

Thanks in advance. Thanks.

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 ing correct if I pass only one input parameter . If I pass two values in input , the 2nd value result overrides the first one . Any help on how can I get response for all the list of inputs with the overlapping part.

Also , I need to sort the input values before calling the chaincode , like if I get Feb Mar Jan in the input , I should sort it as Jan Feb Mar and then run the for loop.

Any help on this is appreciated.

Thanks in advance. Thanks.

Share Improve this question edited Aug 1, 2018 at 4:21 newTask asked Jul 26, 2018 at 6:43 newTasknewTask 871 gold badge2 silver badges9 bronze badges 3
  • 1 What exactly you means by parsing, some kind of string manipulation or what ? – Anirudha Gupta Commented Jul 26, 2018 at 6:46
  • Possible duplicate of Parse Array of JSON objects in NodeJS – Helen Commented Jul 26, 2018 at 6:53
  • Can you console.log(req.body.transaction) and see is that what you want – Sridhar Commented Jul 26, 2018 at 7:37
Add a ment  | 

2 Answers 2

Reset to default 2

You can use body-parser in your app.js like this:

app.use(bodyParser.urlencoded({ 
     extended: true 
}));

Let's say you are receiving array in the post request body with the key: users you will parse it like this:

req.body.users

(It contains the users array)

Do you want to 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}` )
  }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744145621a4560406.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信