ES6 classes and meteor methods - OOP, overriding

so I want to use ES6: arrow functions and classes (cause I think it could be a good way to implement OOP overriding as I have lot of similar code in two methods). but meteor (babel) says there are errors, here’s the code
base(_id, id) => { this._id = _id this.id = id check(_id, String); check(id, String); // Show the latency compensations Meteor._sleepForMs(500); // add user authorization const createdAt = new Date(); const author = 'The User'; // need to impelment User accounts const like = {_id, id, author, createdAt}; db() { // what error ???? it's ES6 class Likes.insert(like); } };

export default function () {
  Meteor.methods({ // refactor: add - remove OOP, whether post or comment (id) handle in client
      'likes.add' = new this.base(_id, id);
      'likes.remove' = new this.base(_id, id) {
        db() => {
          Likes.remove(like);
        }
      }
    }
  });
}

my question is how to use it the right way. And why the fuck is this formatting so ugly? I put the code in code in “code” element tried ctrl+shift+c but still the same

Because you are using inline code-ticks, instead of a code block. Use 3 back ticks on it’s own line instead of a single back tick on a content line.

base(_id, id) => {
  this._id = _id
  this.id = id
  check(_id, String);
  check(id, String);
  // Show the latency compensations
  Meteor._sleepForMs(500);
  // add user authorization
  const createdAt = new Date();
  const author = 'The User'; // need to impelment User accounts
  const like = {_id, id, author, createdAt};
  db() { // what error ???? it's ES6 class
    Likes.insert(like);
  }
};    

ok thanks, but I was mainly asking about the coding stuff :smiley: btw I put 3 backticks on the solo line, then code and after code block 3 backticks again but it still don’t work

As for your real question, the arrow syntax you are using, is mostly used for anonymous function callbacks. So now it’s not clear what you’re trying to do with base.

If you just want to define a function called base, define it like:

function base (_id, id) {
  // ....
}

Or if you really want to stick to ES6 syntax, although I cannot say this is really an improvement in readability:

const base = (_id, id) => {
  // ...
}

Remember, being able to use ES6, doesn’t mean that you must use it’s syntax everywhere, and that you must replace all function definitions with ES6 arrow syntax. Use it where appropriate.

If db is an ES6 class like you are saying, (defined with class db {}) then you must construct an instance before calling methods on it. It’s now not really clear what you are trying to do there. Define a function in local scope? Or call a method with a callback?

When defining inline; the correct syntax would be:

const db = (like) => {
  Likes.insert(like);
}

If you’re trying to call a method named db with a callback function, the correct syntax would be:

db(() => {
  Likes.insert(like);
});

As for the meteor method; it’s an object, so use : instead of = to assign methods to the keys. And don’t use line terminators (;) but use , instead.

I’m not sure what you are trying to do on the likes.remove method. As base doesn’t have an callback, I assume you’re trying to call the base method and add some extra logic. In that case; wrap it in an anonymous function.

export default function () {
  Meteor.methods({ // refactor: add - remove OOP, whether post or comment (id) handle in client
    'likes.add': base(_id, id),
    'likes.remove': (_id, id) => {
      base(_id, id);
      db(() => {
        Likes.remove(like);
      });
    }
  });
}

I would advise you to learn more about ES6, See these sites for example:

no I want to override the db function in base function - it is defined with Likes.insert and in 2nd method I want to do Likes.remove so I need to override db function in base function

In that case you’re looking for a callback.

const base = (_id, id, cb) => {
  cb = cb || _.noop;

  // ...
  const like = {_id, id, author, createdAt};
  cb(like);
}

Meteor.methods({
  'likes.add': base(_id, id, (like) => {
    Likes.insert(like);
  }),
  'likes.remove': base(_id, id, (like) => {
    Likes.remove(like);
  })
});

It’s a matter of taste, but I don’t find the code above very clear. It’s not “oop” either, as that is your real “goal”. I would suggest changing it to something like:

Meteor.methods({
  'likes.add': (_id, id) => {
    const like = getLike(_id, id); // your 'base'
    Likes.insert(like);
  },
    
  'likes.remove': (_id, id) => {
    const like = getLike(_id, id); // your 'base'
    Likes.remove(like);
  }
});

And even than, your base method is not really code that is shared between an insert and a remove method. This topic seems more a general javascript / es6 / learn oop question, than a meteor related question. So I don’t think this is a subject where this forum is meant for. I think stackoverflow can be of greater help for you.

Another (interactive) website that can greatly help you in learning ES6.

https://tutor.mantrajs.com

thanks I know mantra (arunoda’s work)
here’s how I solve it
`
import {Likes} from ‘/lib/collections’;
import {Meteor} from ‘meteor/meteor’;
import {check} from ‘meteor/check’;

class Insert {
   constructor(_id, id) {
      this._id = _id;
      this.id = id;
      check(this._id, String);
      check(this.id, String);
      // Show the latency compensations
      Meteor._sleepForMs(500);
      // XXX: Do some user authorization
      const createdAt = new Date();
      const author = 'The User'; // need to impelment User accounts
      const like = [this._id, this.id, author, createdAt];
      }

    db() {
       Likes.insert(this.like);
        }
    };

  class Remove extends Insert {
      db(){
        Likes.remove(this.like);
      }
  }

export default function () {
  Meteor.methods({
  'likes.add': new Insert(_id, id), // why that error ??
  'likes.remove': new Insert(_id, id)
  });
}

`

If you would like to use OOP then it is better to learn some patterns like active record, repository etc