Reputation: 3
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);
Upvotes: 0
Views: 204
Reputation: 719
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);
});
Upvotes: 1