错误的意外混叠' this'到本地变量 @typescript-eslint/no-this-alias

发布于 2025-02-13 19:35:54 字数 1393 浏览 0 评论 0原文

如何解决此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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一城柳絮吹成雪 2025-02-20 19:35:54

通常存在此警告,以使脚本作者使用箭头功能,而不是声明新变量。例如,

let that = this;
someFn(function(arg) {
  return that.foo = arg;
});

可以简化

someFn(arg => this.foo = arg);

此:但就您而言,您甚至没有在任何其他功能中使用重新分配值 - 您只是在代码中直接引用它,这使得分配完全多余。

只需删除

const self = this;

self.createEvent(

this.createEvent(

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(
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文