Async help with S3

Hello,

I am having an issue getting data from the server side to the client side. I am getting the appropriate info from AWS and can display it on the console, however it will not return to the client side.

Server side code::

Meteor.methods({
  getBucket(){
    var params = {
      Bucket: 'datajournal',
    };
    s3 = new AWS.S3();
    var test;
    s3.listObjects(params, (err,data)=> {
      if(err){
        return err;
      }
      else {
        console.log(data)
        return data;
      }
    });
  }
});

Client Side Code:::

Template.ShowBucket.onCreated(
  function loadBuckets(){
    this.list = new ReactiveVar([]);
    Meteor.call('getBucket', (error,result)=> {
      this.list.set(result);
    });
  }
);

Template.ShowBucket.helpers({
  bucketItem: () => {
    return Template.instance().list.get();

  }
})

As you noted, the issue is due to the data being retrieved in the async callback. One way to fix this is to use Meteor.wrapAsync:

Meteor.methods({
  getBucket(){
    var params = {
      Bucket: 'datajournal',
    };
    s3 = new AWS.S3();
    var test;
    var listObjects = Meteor.wrapAsync(s3.listObjects);
    var data = listObjects(params);
    console.log(data)
    return data;
  }
});

From what I understand, the AWS SDK support promises, so you can also use that instead:

Meteor.methods({
  async getBucket(){
    var params = {
      Bucket: 'datajournal',
    };
    s3 = new AWS.S3();
    var test;
    var data = await s3.listObjects(params).promise();
    console.log(data)
    return data;
  }
});

Here’s some more info on async methods in Meteor:

1 Like

Awesome! Thank you. Many of the answers I were finding online seemed much more complicated then need be.