[英]ERROR Unexpected aliasing of 'this' to local variable @typescript-eslint/no-this-alias
How do I solve this typescript error?如何解决这个打字稿错误?
I'm getting error in the below line.我在下面的行中遇到错误。
const self = this;常量自我 = 这个;
I'm getting error in the terminal like:我在终端中遇到错误,例如:
error Unexpected aliasing of 'this' to local variable @typescript-eslint/no-this-alias错误 'this' 到局部变量 @typescript-eslint/no-this-alias 的意外别名
Please find the code below:请在下面找到代码:
notifyOwner(req, metadata, callback) {
const currentUserId = req.session.identity.individual.userName;
const docId = metadata.docId;
let message = this.templates.selfRemoval;
const self = this;
const targetUserId = metadata.ownerId;
const paperTitle = metadata.title.replace(/<(.|\n)*?>/g, '');
const nonOwnerRole = metadata.userRole;
message = message.replace(/\[CollaboratorName\]/g, req.session.identity.individual.firstName + ' ' + req.session.identity.individual.lastName);
message = message.replace(/\[NonOwnerRole\]/g, (nonOwnerRole === 'author') ? 'collaborator' : 'reviewer');
message = message.replace(/\[PaperTitle\]/g, paperTitle);
const eventData = {
message: message,
objectType: 'manuscript',
objectId: docId
};
self.createEvent(currentUserId, targetUserId, eventData, (err, result) => {
if (result) {
const userActivityService = new UserActivityService();
userActivityService.broadcastRefreshUserEvents(eventData['objectId'], { userId: targetUserId });
}
callback(null, JSON.stringify({ notified: true }));
});
}
This warning usually exists to get script-writers to utilize arrow functions instead of declaring new variables.这个警告通常是为了让脚本编写者使用箭头函数而不是声明新变量。 For example, this:
例如,这个:
let that = this;
someFn(function(arg) {
return that.foo = arg;
});
can be simplified to可以简化为
someFn(arg => this.foo = arg);
But in your case, you're not even using the reassigned value in any other functions - you're just referencing it directly lower in the code, which makes the assignment completely superfluous.但是在您的情况下,您甚至没有在任何其他函数中使用重新分配的值 - 您只是在代码中直接引用它,这使得分配完全多余。
Just remove your只需删除您的
const self = this;
and replace并更换
self.createEvent(
with和
this.createEvent(
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.