Tips for transferring values from listObjects to an array

I have an array called yourArray, defined as var yourArray = [];. My goal is to populate this array with object names retrieved from the s3 using listObjects.

var params ={
     Bucket: 'exBucket',
     Prefix: 'somePrefix'
};

s3.listObjects(params, function(Err, Data){
    if(!Err){
      for (var i = 0; i < Data.Contents.length; i++){
         console.log('Listed: ', Data.Contents[i].Key);
         yourArray.push(Data.Contents[i].Key);
      }
    };
});

While

console.log('Listed: ', Data.Contents[i].Key);
prints all the names correctly,
yourArray.push(Data.Contents[i].Key);
fails to add the names and leaves the array empty. What am I doing wrong?

Answer №1

According to information from Jaromanda X, the function listObjects operates asynchronously, causing your code to attempt inserting a value into an array before the object is fully processed.

To address this issue with S3 objects, I recommend utilizing Bluebird promises for smoother handling.

https://www.npmjs.com/package/aws-promised

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to show <LI> elements

I'm having trouble adding LI items to the #historial <UL> tag. Whenever the for loop is inside the function, the list displays with some elements duplicated. I believe the for loop should remain outside of the function. Could someone please he ...

What is the best way to utilize a JOptionPane constructor for transferring objects from my method to an uninitialized array?

Can someone help me understand how to effectively store objects from the constructor in my Bookstore program? Right now, I am only getting one object and the rest are all null. I would appreciate it if someone could explain this in a way that is easy for ...

What are the reasons for the success function not being called and what steps can be taken to correct it

Whenever I attempt to submit the post, the data is successfully sent and received by the server, but the success function never gets called. In the network inspector tab of Chrome, the connection shows as stalled with a warning: "connection is not finished ...

`json_encode does not output a UTF-8 character`

I send an AJAX request to the PHP server, and receive back an array encoded in JSON. This array has only two indexes. When I log it using console.log(), this is what I see: {"1":"\u00d9\u0081\u00db\u008c\u00d9\u0084\u0 ...

Ways to transfer ID to a different HTML page

I have some Javascript code in a file named nextPage.js. When this code is executed by clicking on something, it should transfer the value of category_id to another page called reportlist.html. Can you please provide assistance with this? var base_url = " ...

What is the process for constructing a regular expression (regex) in JavaScript to validate an id?

My validation requirements are relatively straightforward, but as someone who is new to regex, I am in need of quick assistance. The format should be either 1234567890 or 123456-7890, allowing for any number ranging from 0 to 9 and a total character length ...

What is the best way to collapse a jQuery accordion before expanding another one?

When I click on a new accordion item after opening one, my goal is to have it close the open one first and then open the new one. This is my current code: jQuery('.acclink').click(function () { jQuery("#accordion").accordion("activate", -1) ...

What is the best way to execute ajax functions one after another?

Is there a way to execute ajax functions sequentially without using asynch: false? Using asynch: false can cause the browser to hang, so I'm looking for an alternative method. I have 5 checkboxes, named ch1, ch2, ch3, ch4, ch5, each with a value corre ...

Issues encountered when trying to retrieve data from an Express server

I have set up a NodeJS server with Express.js and I am attempting to send a request to it using fetch. fetch("http://localhost:3001/api", { method: "POST", headers: { "Content-Type": "application/json", ...

deliver a promise with a function's value

I have created a function to convert a file to base64 for displaying the file. ConvertFileToAddress(event): string { let localAddress: any; const reader = new FileReader(); reader.readAsDataURL(event.target['files'][0]); reader ...

Is there a way in JavaScript to convert a web page into a string?

I'm looking for a way to retrieve the HTML content of a webpage using JavaScript, even if it's on a different domain. Similar to what wget does but in JavaScript. I intend to use this for web crawling purposes. Could anyone guide me on how to fe ...

The input-group-btn is positioned to the right of the field, appearing to float

The input-group-btn for the targetDate field remains fixed on the right side of the input group despite screen width variations until I introduce the automatically generated CSS from the hottowel generator. I require assistance in identifying which aspect ...

Leveraging CSS Modules alongside external packages

Below is the important section from my webpack.config.js file: module: { loaders: [ { test: /\.css$/, exclude: /node_modules/, loaders: [ "style-loader?sourceMap", "css-l ...

Data-binding in Angular ceases to function properly in the presence of duplicate values within an

My HTML code is set up to bind the $scope.comments array to an unordered list; <div ng-app="" ng-controller="commentController"> <ul> <li ng-repeat="c in comments"> {{ c }} </li> </ul> < ...

When using nuxt-child, the parent component is not rendered

Here is how my folder structure is set up: |-profile |-- index.vue |-- address/index.vue After adding <nuxt-child />, I noticed that the content of profile/index.vue is not rendering properly. Instead, it seems to be loading a completely new route. ...

Is it possible to generate an array by adding random numbers within the range of 1 to 1

I've been trying to figure out how to add numbers from 0 to 99 into an array. However, my assignment requires me to do it for numbers between 1 and 1,000,000. I keep ending up with really large numbers instead of smaller ones. Could it be that the cha ...

When trying to apply styles using ng-style attribute with jQuery, Angular does not seem to

Check out this plunker showcasing the issue : http://plnkr.co/edit/1ceWH9o2WNVnUUoWE6Gm Take a look at the code : var app = angular.module('myApp', []); app.controller('myCtrl', function($scope) { console.log('yeah'); ...

Testing the dispatching of a custom event in a method

I am facing an issue with a static method I have that is responsible for creating a custom event and dispatching it: class MyComponent extends { static refreshComponent() { const event = new Event('refreshComponent'); ...

Is there a way to retrieve a particular component from an HTML template in VueJS?

After successfully integrating VueJS into my Django project using webpack loader, I have encountered some difficulties due to being new to Vue and struggling with understanding its structure. Here is the current template: django_template.html: where the ...

Activating two buttons in jquery will trigger this action

I am currently working on developing a filter button that will perform different actions based on which buttons are pressed. Pressing button1 will trigger one action, while pressing button2 will trigger another action. If button1 is pressed first, followe ...