在If_Else条件下为循环实现多个功能?

时间:2018-09-13 10:48:16

标签: javascript node.js for-loop if-statement

用例

我有function1,它调用API1并给出如下的S输出

{ Buckets:
   [ { Name: 'ONE',
       CreationDate: 2018-09-12T11:32:04.000Z },
     { Name: 'SEVEN',
       CreationDate: 2018-09-12T10:06:44.000Z },
     { Name: 'THREE',
       CreationDate: 2018-09-12T10:06:56.000Z },
     { Name: 'FOUR',
       CreationDate: 2018-09-12T10:07:06.000Z },
     { Name: 'FIVE',
       CreationDate: 2018-09-12T10:07:18.000Z },
     { Name: 'SIX',
       CreationDate: 2018-09-12T10:07:28.000Z },
     { Name: 'TWO',
       CreationDate: 2018-09-12T10:07:39.000Z } ],
}

和function2,它们将从功能1获取存储桶名称,并根据for循环中的每次迭代动态地传递到params1

{"ResponseMetadata":{"RequestId":"a7c64a98-b735-11e"},"MetricDataResults":[{"Id":"m17","Label":"BucketSizeBytes","Timestamps":["2018-09-04T18:30:00.000Z"],"Values":[2048717],"StatusCode":"Complete","Messages":[]}]
{"ResponseMetadata":{"RequestId":"a7c64a98-b735-11e"},"MetricDataResults":[{"Id":"m17","Label":"BucketSizeBytes","Timestamps":["2018-09-04T18:30:00.000Z"],"Values":[2048718],"StatusCode":"Complete","Messages":[]}]
{"ResponseMetadata":{"RequestId":"a7c64a98-b735-11e"},"MetricDataResults":[{"Id":"m17","Label":"BucketSizeBytes","Timestamps":["2018-09-04T18:30:00.000Z"],"Values":[2048719],"StatusCode":"Complete","Messages":[]}]
{"ResponseMetadata":{"RequestId":"a7c64a98-b735-11e"},"MetricDataResults":[{"Id":"m17","Label":"BucketSizeBytes","Timestamps":["2018-09-04T18:30:00.000Z"],"Values":[2048720],"StatusCode":"Complete","Messages":[]}]
{"ResponseMetadata":{"RequestId":"a7c64a98-b735-11e"},"MetricDataResults":[{"Id":"m17","Label":"BucketSizeBytes","Timestamps":["2018-09-04T18:30:00.000Z"],"Values":[2048721],"StatusCode":"Complete","Messages":[]}]
{"ResponseMetadata":{"RequestId":"a7c64a98-b735-11e"},"MetricDataResults":[{"Id":"m17","Label":"BucketSizeBytes","Timestamps":["2018-09-04T18:30:00.000Z"],"Values":[2048722],"StatusCode":"Complete","Messages":[]}]
{"ResponseMetadata":{"RequestId":"a7c64a98-b735-11e"},"MetricDataResults":[{"Id":"m17","Label":"BucketSizeBytes","Timestamps":["2018-09-04T18:30:00.000Z"],"Values":[2048723],"StatusCode":"Complete","Messages":[]}]

这意味着对于函数1中的每个名称,它都会给出一个类似于above(i.e.7)的Response。马上,我将这两个json合并为一个json,就像这样

{"Name":"test-pics-folder-two","CreationDate":"2018-09-12T10:07:39.000Z","ResponseMetadata":{"RequestId":"fc77226d-b72c-11e8-ad06-0dea5c42ce8a"},"MetricDataResults":[{"Id":"m17","Label":"BucketSizeBytes","Timestamps":["2018-09-04T18:30:00.000Z"],"Values":[2048717],"StatusCode":"Complete","Messages":[]}]}

为此,我编写了如下的NODE.JS代码

var AWS = require('aws-sdk');
// Set the region 
AWS.config.update({
    region: "ap-south-1"
   });

// Create S3 service object
s3 = new AWS.S3();
var cloudwatch = new AWS.CloudWatch({apiVersion: '2010-08-01'});
var params= {};
 s3.listBuckets(params, bucketList); -->Trigger function1
function bucketList(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else   
  {   
      var size = Object.keys(data.Buckets).length; -->Where data has the function 1 output like i mentioned above. And i am taking length of it(i.e.7)
    for(var i = 0; i < size;i++){
       var Json1 = data.Buckets[i]
         console.log(Json1)
       var BucketName = data.Buckets[i].Name
       var params1 = {
        EndTime: 1536128999, /* required */
        MetricDataQueries: [ /* required */
            {
                Id: 'm17', /* required */
                MetricStat: {
                  Metric: { /* required */
                    Dimensions: [
                    //   {
                    //     Name: 'BucketName', /* required */
                    //     Value: 'test-pics-folder' /* required */
                    //   },
                      {
                        Name: 'BucketName', /* required */
                        Value: BucketName /* required */
                      },
                      {
                        Name: 'StorageType', /* required */
                        Value: 'StandardStorage' /* required */
                      }
                      /* more items */
                    ],
                    MetricName: 'BucketSizeBytes',
                    Namespace: 'AWS/S3'
                  },
                  Period: 86400, /* required */
                  Stat: 'Sum', /* required */
                  Unit: 'Bytes'
                },
                ReturnData: true
              },
              /* more items */
            ],
            StartTime: 1536085800, /* required */

            // ScanBy: TimestampDescending | TimestampAscending
          };

            cloudwatch.getMetricData(params1,metricAll) -->Trigger function2
            function metricAll(err, data1) {
              if (err) {
                  console.log(err, err.stack); 
              }
              else     
              {
                  var Json2 = data1  -->The function2 output as in the above mentioned format
               var obj = Object.assign(Json1, Json2);
               console.log(JSON.stringify(obj))       
              }        
            };        

    }
 }       
}

我正在得到这样的输出

    {"Name":"TWO","CreationDate":"2018-09-12T10:07:39.000Z","ResponseMetadata":{"RequestId":"a7c64a98-b735-11e"},"MetricDataResults":[{"Id":"m17","Label":"BucketSizeBytes","Timestamps":["2018-09-04T18:30:00.000Z"],"Values":[2048717],"StatusCode":"Complete","Messages":[]}]
    {"Name":"TWO","CreationDate":"2018-09-12T10:07:39.000Z","ResponseMetadata":{"RequestId":"a7c64a98-b735-11e"},"MetricDataResults":[{"Id":"m17","Label":"BucketSizeBytes","Timestamps":["2018-09-04T18:30:00.000Z"],"Values":[2048718],"StatusCode":"Complete","Messages":[]}]
    {"Name":"TWO","CreationDate":"2018-09-12T10:07:39.000Z","ResponseMetadata":{"RequestId":"a7c64a98-b735-11e"},"MetricDataResults":[{"Id":"m17","Label":"BucketSizeBytes","Timestamps":["2018-09-04T18:30:00.000Z"],"Values":[2048719],"StatusCode":"Complete","Messages":[]}]
    {"Name":"TWO","CreationDate":"2018-09-12T10:07:39.000Z","ResponseMetadata":{"RequestId":"a7c64a98-b735-11e"},"MetricDataResults":[{"Id":"m17","Label":"BucketSizeBytes","Timestamps":["2018-09-04T18:30:00.000Z"],"Values":[2048720],"StatusCode":"Complete","Messages":[]}]
    {"Name":"TWO","CreationDate":"2018-09-12T10:07:39.000Z","ResponseMetadata":{"RequestId":"a7c64a98-b735-11e"},"MetricDataResults":[{"Id":"m17","Label":"BucketSizeBytes","Timestamps":["2018-09-04T18:30:00.000Z"],"Values":[2048721],"StatusCode":"Complete","Messages":[]}]
    {"Name":"TWO","CreationDate":"2018-09-12T10:07:39.000Z","ResponseMetadata":{"RequestId":"a7c64a98-b735-11e"},"MetricDataResults":[{"Id":"m17","Label":"BucketSizeBytes","Timestamps":["2018-09-04T18:30:00.000Z"],"Values":[2048722],"StatusCode":"Complete","Messages":[]}]
    {"Name":"TWO","CreationDate":"2018-09-12T10:07:39.000Z","ResponseMetadata":{"RequestId":"a7c64a98-b735-11e"},"MetricDataResults":[{"Id":"m17","Label":"BucketSizeBytes","Timestamps":["2018-09-04T18:30:00.000Z"],"Values":[2048723],"StatusCode":"Complete","Messages":[]}]

在其中附加函数1的最后一个BucketName并将其分配给function2的所有结果。将console.log(i)保留在

function metricAll(err, data1) {
          if (err) {
              console.log(err, err.stack); 
          }
          else     
          {
                  console.log(i)     
          }        
        };

我得到了[7 7 7 7 7 7 7 7]的奇怪输出。这意味着它仅将for循环的最后一个值传递给它。我不知道为什么?

感谢您的帮助

谢谢

0 个答案:

没有答案