Sailsjs 0.12 - 所有模型的自定义方法
Sailsjs 0.12 - Custom method for all models
我正在尝试向我的 Sails 0.12 应用程序添加 Laravel-like Mass Assign。
到目前为止我已经做了:
将方法 secureUpdate
添加到 config\models.js
:
console.log(this);
var self = this;
var modelName = self.adapter.identity.charAt(0).toUpperCase() + self.adapter.identity.slice(1);
我正在尝试获取调用模型名称,就像此要点(用于播种数据)中的 seed
方法一样:https://gist.github.com/juanpasolano/5c7596d8629eeeb8debd#file-config-models-js
但是this
的值不同,没有适配器对象(undefined)
我不明白为什么this
的值不一样。你能帮帮我吗?
我把答案写在这里只是为了以后可能遇到同样问题的其他人:
this
是 undefined
因为您将 方法 定义为箭头函数,您必须使用 函数表达式,来自 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.
我正在尝试向我的 Sails 0.12 应用程序添加 Laravel-like Mass Assign。
到目前为止我已经做了:
将方法 secureUpdate
添加到 config\models.js
:
console.log(this);
var self = this;
var modelName = self.adapter.identity.charAt(0).toUpperCase() + self.adapter.identity.slice(1);
我正在尝试获取调用模型名称,就像此要点(用于播种数据)中的 seed
方法一样:https://gist.github.com/juanpasolano/5c7596d8629eeeb8debd#file-config-models-js
但是this
的值不同,没有适配器对象(undefined)
我不明白为什么this
的值不一样。你能帮帮我吗?
我把答案写在这里只是为了以后可能遇到同样问题的其他人:
this
是 undefined
因为您将 方法 定义为箭头函数,您必须使用 函数表达式,来自 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.