错误的意外混叠' this'到本地变量 @typescript-eslint/no-this-alias
如何解决此Typescript错误?
我在下行中遇到了错误。
const self = this;
我在终端中遇到错误:
错误意外的'this'与local variable @typecript-eslint/no-this-this-alias
请找到以下代码:
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 }));
});
}
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
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 }));
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常存在此警告,以使脚本作者使用箭头功能,而不是声明新变量。例如,
可以简化
此:但就您而言,您甚至没有在任何其他功能中使用重新分配值 - 您只是在代码中直接引用它,这使得分配完全多余。
只需删除
您
的
This warning usually exists to get script-writers to utilize arrow functions instead of declaring new variables. For example, this:
can be simplified to
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
and replace
with