节点js中数组对象的重叠

Overlapping of array objects in node js

我在请求正文中得到一个数组,如:

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

此输入有两个参数(month:enumyear:string)。

我需要遍历这个数组并调用链代码,最后发送响应。我做了以下事情:

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);
  }

但如果我只传递一个输入参数,响应就会正确。如果我在输入中传递两个值,则第二个值结果会覆盖第一个值。关于如何获得所有具有重叠部分的输入列表的响应的任何帮助。

此外,我需要在调用链代码之前对输入值进行排序,就像如果我在输入中得到 Feb Mar Jan,我应该将其排序为 Jan Feb Mar,然后 运行 for 循环。

如有任何帮助,我们将不胜感激。

提前致谢。

试试这个代码可能适合你

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);

输出:

[ { chaincodeId: 'abc',
    fcn: 'getTransactionsByKey',
    args: 'JAN2018_new' },
  { chaincodeId: 'abc',
    fcn: 'getTransactionsByKey',
    args: 'JUL2018_new' },
  { chaincodeId: 'abc',
    fcn: 'getTransactionsByKey',
    args: 'MAR2018_new' } ]

创建所有请求对象的数组并将其传递给 queryByChaincode。

var requests = req.body.map((body) => {
    return {
        chaincodeId: 'abc',
        fcn: 'getTransactionsByKey',
        args: [`${body.month}${body.year}_new`]
    };
});
return channel.queryByChaincode(requests);