Migration to 2.8 for dummies

Hello,
I would like to start the migration to 2.8 but I am a bit lost.
The most common pattern we have in our code is calling a method from the client when submitting a form for example. I would like to start migrating this code.

For example

Template.myBlazeTemplate.events({
  'click a#new-team': function (e) {
    e.preventDefault();
    Meteor.call('new-team', function (err, teamId) {
          if (err) {
            console.error(err);
            toastr.error(TAPi18n.__(err.reason));
            return;
          }
          toastr.success(TAPi18n.__('Team created'));
          Session.set('teamId', teamId);
        });
  },
});

Does this code on the client needs to be changed ?

On the server (server only) we define the method that interact with the DB

Meteor.methods({
  'new-team': function (teamName) {
    check(teamName, String);
   return Teams.insert({teamName})
  },
});

Do we have to change this code on the server ?

The documentation of 2.8 still say that this is good to do it like this : Methods | Meteor Guide but the migration guide says to change code asap

Thanks for your help.

Template.myBlazeTemplate.events({
  'click a#new-team': e => {
    e.preventDefault();
    Meteor.callAsync('new-team')
      .then(teamId => {
            toastr.success(TAPi18n.__('Team created'));
            Session.set('teamId', teamId);
      })
      .catch(err => {
            console.error(err);
            toastr.error(TAPi18n.__(err.reason));
            return;
      })
  }
});

Meteor.methods({
  'new-team': function (teamName) {
    check(teamName, String);
   return await Teams.insertAsync ({teamName})
  },
});

1 Like

Thanks.
ĂŹt should be :

Meteor.methods({
  'new-team': async function (teamName) {
    check(teamName, String);
   return await Teams.insertAsync ({teamName})
  },
});

missing async. Right ?

1 Like

:slight_smile: right as it doesn’t make async sense otherwise. My mistake :slight_smile:

If I understand well, everywhere where I use findOne, update, … I have to replace with the findOneAsync, … and add the keywords async and await.
This is an enormous amount of code rewrite !

Yeah, I started early with the 2.8 betas. I’d say that it may be a lot but it goes pretty fast. It is more a re-syntax rather than re-write.

1 Like

On the client in a Blaze helper, should I rewrite the code too ?

Template.myBlazeTemplate.helpers({
  team: function() {
    return Teams.FindOne(this.teamId);
  },
});

should become this ?

Template.myBlazeTemplate.helpers({
  team: async function() {
    return await Teams.FindOneAsync(this.teamId);
  },
});

Thanks

@dokithonon Blaze helpers are based on Tracker, which does not support asynchronous functions (Tracker Async PR.

1 Like

You shouldn’t need to rewrite client code. Callbacks are async already, no need to convert them to use promises (at least I think that is the case)

I’d prefer to leave behind the callback syntax and stay more future proof with .then() and .catch()

1 Like

Yes, that’s fine if you have the time. We have been using callAsync on the client already, so don’t have many callbacks