[英]How to ignore a block of code when transpiling with BabelJs?
I'm trying to reference this
in ES2015, with Babeljs. 我想引用
this
在ES2015,与Babeljs。 This problem I am having is Babeljs keeps moving this
out of the scope into the external scope. 我遇到的问题是Babeljs不断将
this
从范围移到外部范围。 This breaks the code. 这会破坏代码。 I am hoping Babeljs has a comment, or some sort of block notation I can use to have it ignore the code so it won't be transpiled.
我希望Babeljs有一个注释,或者某种形式的块符号,我可以用它来忽略代码,以免被编译。
If there are any Mongoose guru's out there, maybe there is another way to access the property and function in question ( this.isNew || this.isModified('email')
). 如果那里有猫鼬大师,那么也许还有另一种访问相关属性和函数的方法(
this.isNew || this.isModified('email')
)。
Here is the ES2015 code. 这是ES2015代码。
setDuplicateEmailValidation(){
this.schema.path('email').validate((email, fn) => {
let User = mongoose.model('User');
// Check only when it is a new user or when email field is modified
if (this.isNew || this.isModified('email')) {
User.find({ email: email }).exec((err, users) => {
fn(!err && users.length === 0);
});
} else fn(true);
}, 'Email already exists');
}
In the if-statement if (this.isNew || this.isModified('email'))
the pre-transpiled code has references to this
. 在if语句
if (this.isNew || this.isModified('email'))
,预编译的代码具有this
引用。 The scope of validate()
is important because while in this scope I have access to Mongoosejs's Document API. validate()
的范围很重要,因为在此范围内,我可以访问Mongoosejs的Document API。 Once the code moves out of the validate()
scope I no longer have access to the Document API. 一旦代码移出
validate()
范围,我将不再有权访问Document API。
Transpiled code. 转译的代码。
function setDuplicateEmailValidation() {
var _this = this;
this.schema.path('email').validate(function (email, fn) {
var User = _mongoose2['default'].model('User');
// Check only when it is a new user or when email field is modified
if (_this.isNew || _this.isModified('email')) {
User.find({ email: email }).exec(function (err, users) {
fn(!err && users.length === 0);
});
} else fn(true);
}, 'Email already exists');
}
}
In this code, you'll notice that the if-statement references a variable ( if (_this.isNew || _this.isModified('email'))
) that is outside the scope of the validate function. 在此代码中,您会注意到if语句引用了超出validate函数范围之外的变量(
if (_this.isNew || _this.isModified('email'))
)。 Because of this (no pun intended) move, I am losing access to Mongoosejs's Document API. 由于此举(没有双关语),我无法访问Mongoosejs的Document API。
Any suggestions will be greatly appreciated. 任何建议将不胜感激。
Don't use arrow function , just use the function
keyword: 不要使用箭头功能 ,只需使用
function
关键字:
this.schema.path('email').validate(function (email, fn) {
An arrow function expression (also known as fat arrow function) has a shorter syntax compared to function expressions and lexically binds the this value .
箭头函数表达式(也称为胖箭头函数)的语法比函数表达式短,并且在词法上绑定了this值 。
Emphasis mine. 强调我的。 This means that
this
inside the arrow function will be this
in the same lexical context outside the arrow function. 这意味着,
this
箭头函数内将this
在箭头功能之外的相同的词汇上下文。 This is intentional and different from function
syntax. 这是有意的,与
function
语法不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.