Duc Anh
Duc Anh

Reputation: 581

Sailsjs 0.12 - Custom method for all models

I am trying to add Laravel-like Mass Assign to my Sails 0.12 application.

So far I have done:

Add method secureUpdate to config\models.js:

console.log(this);

var self = this;
var modelName = self.adapter.identity.charAt(0).toUpperCase() + self.adapter.identity.slice(1);

I am trying to get the calling model name, just like in seed method in this gist(for seeding data): https://gist.github.com/juanpasolano/5c7596d8629eeeb8debd#file-config-models-js

But the value of this is diffirent, with no adapter object(undefined)

I do not understand why the value of this is not the same. Could you help me out?

Upvotes: 0

Views: 114

Answers (1)

Hamza Fatmi
Hamza Fatmi

Reputation: 1255

I write the answer here just for others who may have the same problem in the future :

The this is undefined because you are defining the method as an arrow function, you have to use a function expression, from MDN :

An arrow function expression has a shorter syntax than a function expression and does not have its own this, arguments, super, or new.target. These function expressions are best suited for non-method functions, and they cannot be used as constructors.

Upvotes: 1

Related Questions