首页 > 解决方案 > 'err' 已在上层范围内声明

问题描述

我的代码没有给出任何错误,只是我必须满足 eslint,因为它给出的错误 'err' 已经在上层范围内声明。 如何在我的代码中修复它。

describe('/GET/:ID', () => {
  it('should Get the task by ID', (done) => {
    const book = new Task({ task: 'The Lord of the Rings' });
    book.save((err, task) => {
      chai.request(server)
        .get(`/task/${task.id}`)
        .send(task)
        .end((err, res) => {
          expect(res).to.have.status(200);
          done();
        });
    });
  });
});

标签: node.jsexpressmocha.jseslintchai

解决方案


ESlint 警告你变量 shadowing,不是因为它会抛出错误或不起作用,只是因为有时它可能是无意的,从而导致意外的行为,例如你想在回调中使用errfrom ?book.savechai

要修复,只需使用更明确的命名约定,例如saveErr/chaiErr


推荐阅读