0

I am new in node.js.

I made a function that returns an array of objects.

When I try to display it in the console, it shows the message Undefined.

Here is the function:

async function allBucketKeys(s3, bucket) {
    const params = {
      Bucket: bucket,
    };

    var keys = [];
    for (;;) {
      var data = await s3.listObjects(params).promise();

      data.Contents.forEach((elem) => {
        keys = keys.concat(elem.Key);
      });

      if (!data.IsTruncated) {
        break;
      }
      params.Marker = data.NextMarker;
    }
    console.log(keys);

    return keys;
  } 

  var s3 = new AWS.S3()

 array = allBucketKeys(s3, "mymplifyroject-20190123180140-deployment").keys;

 console.log(array);

1 Answer 1

1

You're returning a promise from your async function, so you have to await it or call .then on it before you can access the value.

async function allBucketKeys(s3, bucket) {
  const params = {
    Bucket: bucket
  };

  var keys = [];
  for (;;) {
    var data = await s3.listObjects(params).promise();

    data.Contents.forEach(elem => {
      // You might want to use keys.push(elem.Key) here, but if elem.Key
      // is an array it would have a different behavior
      keys = keys.concat(elem.Key);
    });

    if (!data.IsTruncated) {
      break;
    }
    params.Marker = data.NextMarker;
  }
  console.log(keys);

  return keys;
}
var s3 = new AWS.S3();
allBucketKeys(s3, "mymplifyroject-20190123180140-deployment").then(keys => {
  console.log(keys);
});
Sign up to request clarification or add additional context in comments.

1 Comment

is there any other method to export it to a "jsx" file and display its contents

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.