These are chat archives for Automattic/mongoose

7th
Oct 2016
Roy
@dxcqcv
Oct 07 2016 02:03
hi, everyone, if I wanna use arrow function in customs method to model, how to make this refers to model itself, not to refer to method, for example userSchema.methods.setPassword = (password) => { this.salt = SALT_VALUE; this.hash=HASH_VALUE; }
in this case, this will refer to setPassword method, but userSchema model itself.
Paul "Joey" Clark
@joeytwiddle
Oct 07 2016 02:05
You can’t set this of an arrow function. Use a normal function.
And it seems the old var self = this; trick won’t work in this circumstance.
In Coffeescript you could use -> instead of => but ES6 only gave us one type of arrow function.
Roy
@dxcqcv
Oct 07 2016 02:09
so no other something can representative model itself? like model object or something?
in arrow function
something like model.salt
to delegate this.salt
Paul "Joey" Clark
@joeytwiddle
Oct 07 2016 02:11
Where will you get model from? You could pass it into the function.
user.setPassword(password, user);
Roy
@dxcqcv
Oct 07 2016 02:14
OK, no inside self model, pass it too inconvenient, I'll use a normal function
thank you @joeytwiddle
Paul "Joey" Clark
@joeytwiddle
Oct 07 2016 02:15
Oh don’t thank me. Thank ECMA ;)
Roy
@dxcqcv
Oct 07 2016 02:17
hahaha :smile: